About the project

This all seems so confusing at first

Write a short description about the course and add a link to your github repository here. This is an R markdown (.Rmd) file so you can use R markdown syntax. See the ‘Useful links’ page in the mooc area (chapter 1) for instructions.

The course is all about exploring the possibilities of ODS and getting familiar with, as well as having fun

My_repository


Chapter 2 - Regression and model validation

I have created an appropriate analysis dataset and excluded unwanted observations. Here we have analysis on the dataset and it’s variables. - Describe your work and results clearly. - Assume the reader has an introductory course level understanding of writing and reading R code as well as statistical methods - Assume the reader has no previous knowledge of your data or the more advanced methods you are using

For these excercises the libraries “dplyr”, “ggplot2” and “GGally” are necessary. When reading the code these need to be installed and read. I have also included cache = F, since without it my version of R refused to knit the plots I made

  1. First we read the dataset I created into R and explore its contents, dimensions and structure
cache=F
 library("dplyr")
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library("ggplot2")
library("GGally")
## 
## Attaching package: 'GGally'
## The following object is masked from 'package:dplyr':
## 
##     nasa
library("lattice")
Students2014 <- read.table("~/Documents/IODS-project/data/learning2014/learning2014.txt", header = TRUE, sep = " ")
print(Students2014)
##     gender Age Attitude     deep  stra     surf Points
## 1        F  53       37 3.583333 3.375 2.583333     25
## 2        M  55       31 2.916667 2.750 3.166667     12
## 3        F  49       25 3.500000 3.625 2.250000     24
## 4        M  53       35 3.500000 3.125 2.250000     10
## 5        M  49       37 3.666667 3.625 2.833333     22
## 6        F  38       38 4.750000 3.625 2.416667     21
## 7        M  50       35 3.833333 2.250 1.916667     21
## 8        F  37       29 3.250000 4.000 2.833333     31
## 9        M  37       38 4.333333 4.250 2.166667     24
## 10       F  42       21 4.000000 3.500 3.000000     26
## 11       M  37       39 3.583333 3.625 2.666667     31
## 12       F  34       38 3.833333 4.750 2.416667     31
## 13       F  34       24 4.250000 3.625 2.250000     23
## 14       F  34       30 3.333333 3.500 2.750000     25
## 15       M  35       26 4.166667 1.750 2.333333     21
## 16       F  33       41 3.666667 3.875 2.333333     31
## 17       F  32       26 4.083333 1.375 2.916667     20
## 18       F  44       26 3.500000 3.250 2.500000     22
## 19       M  29       17 4.083333 3.000 3.750000      9
## 20       F  30       27 4.000000 3.750 2.750000     24
## 21       M  27       39 3.916667 2.625 2.333333     28
## 22       M  29       34 4.000000 2.375 2.416667     30
## 23       F  31       27 4.000000 3.625 3.000000     24
## 24       F  37       23 3.666667 2.750 2.416667      9
## 25       F  26       37 3.666667 1.750 2.833333     26
## 26       F  26       44 4.416667 3.250 3.166667     32
## 27       M  30       41 3.916667 4.000 3.000000     32
## 28       F  33       37 3.750000 3.625 2.000000     33
## 29       F  33       25 3.250000 2.875 3.500000     29
## 30       M  28       30 3.583333 3.000 3.750000     30
## 31       M  26       34 4.916667 1.625 2.500000     19
## 32       F  27       32 3.583333 3.250 2.083333     23
## 33       F  25       20 2.916667 3.500 2.416667     19
## 34       F  31       24 3.666667 3.000 2.583333     12
## 35       M  20       42 4.500000 3.250 1.583333     10
## 36       F  39       16 4.083333 1.875 2.833333     11
## 37       M  38       31 3.833333 4.375 1.833333     20
## 38       M  24       38 3.250000 3.625 2.416667     26
## 39       M  26       38 2.333333 2.500 3.250000     31
## 40       M  25       33 3.333333 1.250 3.416667     20
## 41       F  30       17 4.083333 4.000 3.416667     23
## 42       F  25       25 2.916667 3.000 3.166667     12
## 43       M  30       32 3.333333 2.500 3.500000     24
## 44       F  48       35 3.833333 4.875 2.666667     17
## 45       F  24       32 3.666667 5.000 2.416667     29
## 46       F  40       42 4.666667 4.375 3.583333     23
## 47       M  25       31 3.750000 3.250 2.083333     28
## 48       F  23       39 3.416667 4.000 3.750000     31
## 49       F  25       19 4.166667 3.125 2.916667     23
## 50       F  23       21 2.916667 2.500 2.916667     25
## 51       M  27       25 4.166667 3.125 2.416667     18
## 52       M  25       32 3.583333 3.250 3.000000     19
## 53       M  23       32 2.833333 2.125 3.416667     22
## 54       F  23       26 4.000000 2.750 2.916667     25
## 55       F  23       23 2.916667 2.375 3.250000     21
## 56       F  45       38 3.000000 3.125 3.250000      9
## 57       F  22       28 4.083333 4.000 2.333333     28
## 58       F  23       33 2.916667 4.000 3.250000     25
## 59       M  21       48 3.500000 2.250 2.500000     29
## 60       M  21       40 4.333333 3.250 1.750000     33
## 61       F  21       40 4.250000 3.625 2.250000     33
## 62       F  21       47 3.416667 3.625 2.083333     25
## 63       F  26       23 3.083333 2.500 2.833333     18
## 64       F  25       31 4.583333 1.875 2.833333     22
## 65       F  26       27 3.416667 2.000 2.416667     17
## 66       M  21       41 3.416667 1.875 2.250000     25
## 67       F  23       34 3.416667 4.000 2.833333     28
## 68       F  22       25 3.583333 2.875 2.250000     22
## 69       F  22       21 1.583333 3.875 1.833333     26
## 70       F  22       14 3.333333 2.500 2.916667     11
## 71       F  23       19 4.333333 2.750 2.916667     29
## 72       M  22       37 4.416667 4.500 2.083333     22
## 73       M  23       32 4.833333 3.375 2.333333     21
## 74       M  24       28 3.083333 2.625 2.416667     28
## 75       F  22       41 3.000000 4.125 2.750000     33
## 76       F  23       25 4.083333 2.625 3.250000     16
## 77       M  22       28 4.083333 2.250 1.750000     31
## 78       M  20       38 3.750000 2.750 2.583333     22
## 79       M  22       31 3.083333 3.000 3.333333     31
## 80       M  21       35 4.750000 1.625 2.833333     23
## 81       F  22       36 4.250000 1.875 2.500000     26
## 82       F  23       26 4.166667 3.375 2.416667     12
## 83       M  21       44 4.416667 3.750 2.416667     26
## 84       M  22       45 3.833333 2.125 2.583333     31
## 85       M  29       32 3.333333 2.375 3.000000     19
## 86       F  29       39 3.166667 2.750 2.000000     30
## 87       F  21       25 3.166667 3.125 3.416667     12
## 88       M  28       33 3.833333 3.500 2.833333     17
## 89       F  21       33 4.250000 2.625 2.250000     18
## 90       F  30       30 3.833333 3.375 2.750000     19
## 91       F  21       29 3.666667 2.250 3.916667     21
## 92       M  23       33 3.833333 3.000 2.333333     24
## 93       F  21       33 3.833333 4.000 2.750000     28
## 94       F  21       35 3.833333 3.500 2.750000     17
## 95       F  20       36 3.666667 2.625 2.916667     18
## 96       M  22       37 4.333333 2.500 2.083333     17
## 97       M  21       42 3.750000 3.750 3.666667     23
## 98       M  21       32 4.166667 3.625 2.833333     26
## 99       F  20       50 4.000000 4.125 3.416667     28
## 100      M  22       47 4.000000 4.375 1.583333     31
## 101      F  20       36 4.583333 2.625 2.916667     27
## 102      F  20       36 3.666667 4.000 3.000000     25
## 103      M  24       29 3.666667 2.750 2.916667     23
## 104      F  20       35 3.833333 2.750 2.666667     21
## 105      F  19       40 2.583333 1.375 3.000000     27
## 106      F  21       35 3.500000 2.250 2.750000     28
## 107      F  21       32 3.083333 3.625 3.083333     23
## 108      F  22       26 4.250000 3.750 2.500000     21
## 109      F  25       20 3.166667 4.000 2.333333     25
## 110      F  21       27 3.083333 3.125 3.000000     11
## 111      F  22       32 4.166667 3.250 3.000000     19
## 112      F  25       33 2.250000 2.125 4.000000     24
## 113      F  20       39 3.333333 2.875 3.250000     28
## 114      M  24       33 3.083333 1.500 3.500000     21
## 115      F  20       30 2.750000 2.500 3.500000     24
## 116      M  21       37 3.250000 3.250 3.833333     24
## 117      F  20       25 4.000000 3.625 2.916667     20
## 118      F  20       29 3.583333 3.875 2.166667     19
## 119      M  31       39 4.083333 3.875 1.666667     30
## 120      F  20       36 4.250000 2.375 2.083333     22
## 121      F  22       29 3.416667 3.000 2.833333     16
## 122      F  22       21 3.083333 3.375 3.416667     16
## 123      M  21       31 3.500000 2.750 3.333333     19
## 124      M  22       40 3.666667 4.500 2.583333     30
## 125      F  21       31 4.250000 2.625 2.833333     23
## 126      F  21       23 4.250000 2.750 3.333333     19
## 127      F  21       28 3.833333 3.250 3.000000     18
## 128      F  21       37 4.416667 4.125 2.583333     28
## 129      F  20       26 3.500000 3.375 2.416667     21
## 130      F  21       24 3.583333 2.750 3.583333     19
## 131      F  25       30 3.666667 4.125 2.083333     27
## 132      M  21       28 2.083333 3.250 4.333333     24
## 133      F  24       29 4.250000 2.875 2.666667     21
## 134      F  20       24 3.583333 2.875 3.000000     20
## 135      M  21       31 4.000000 2.375 2.666667     28
## 136      F  20       19 3.333333 3.875 2.166667     12
## 137      F  20       20 3.500000 2.125 2.666667     21
## 138      F  18       38 3.166667 4.000 2.250000     28
## 139      F  21       34 3.583333 3.250 2.666667     31
## 140      F  19       37 3.416667 2.625 3.333333     18
## 141      F  21       29 4.250000 2.750 3.500000     25
## 142      F  20       23 3.250000 4.000 2.750000     19
## 143      M  21       41 4.416667 3.000 2.000000     21
## 144      F  20       27 3.250000 3.375 2.833333     16
## 145      F  21       35 3.916667 3.875 3.500000      7
## 146      F  20       34 3.583333 3.250 2.500000     21
## 147      F  18       32 4.500000 3.375 3.166667     17
## 148      M  22       33 3.583333 4.125 3.083333     22
## 149      F  22       33 3.666667 3.500 2.916667     18
## 150      M  24       35 2.583333 2.000 3.166667     25
## 151      F  19       32 4.166667 3.625 2.500000     24
## 152      F  20       31 3.250000 3.375 3.833333     23
## 153      F  20       28 4.333333 2.125 2.250000     23
## 154      F  17       17 3.916667 4.625 3.416667     26
## 155      M  19       19 2.666667 2.500 3.750000     12
## 156      F  20       35 3.083333 2.875 3.000000     32
## 157      F  20       24 3.750000 2.750 2.583333     22
## 158      F  20       21 4.166667 4.000 3.333333     20
## 159      F  20       29 4.166667 2.375 2.833333     21
## 160      F  19       19 3.250000 3.875 3.000000     23
## 161      F  19       20 4.083333 3.375 2.833333     20
## 162      F  22       42 2.916667 1.750 3.166667     28
## 163      M  35       41 3.833333 3.000 2.750000     31
## 164      F  18       37 3.166667 2.625 3.416667     18
## 165      F  19       36 3.416667 2.625 3.000000     30
## 166      M  21       18 4.083333 3.375 2.666667     19
#2.
dim(Students2014)
## [1] 166   7
str(Students2014)
## 'data.frame':    166 obs. of  7 variables:
##  $ gender  : Factor w/ 2 levels "F","M": 1 2 1 2 2 1 2 1 2 1 ...
##  $ Age     : int  53 55 49 53 49 38 50 37 37 42 ...
##  $ Attitude: int  37 31 25 35 37 38 35 29 38 21 ...
##  $ deep    : num  3.58 2.92 3.5 3.5 3.67 ...
##  $ stra    : num  3.38 2.75 3.62 3.12 3.62 ...
##  $ surf    : num  2.58 3.17 2.25 2.25 2.83 ...
##  $ Points  : int  25 12 24 10 22 21 21 31 24 26 ...

Second we display graphical implementation of the data

Overview:

plot(Students2014)

Results without variable ‘gender’: pairs(Students2014[-1])

pairs(Students2014[-1])

Last, a ggpairs-graphic for possibly a more clear display:

ggpairs(Students2014, mapping = aes(col = gender, alpha = 0.3), lower = list(combo = wrap("facethist", bins = 20)))

Next we have summaries for the included variables and scatterplots to clarify their influence on the ‘points’ -variable:

library(ggplot2)
qplot(Attitude, Points, data = Students2014) + geom_smooth(method = "lm")

qplot(Age, Points, data = Students2014) + geom_smooth(method = "lm")

qplot(gender, Points, data = Students2014) + geom_smooth(method = "lm")

qplot(deep, Points, data = Students2014) + geom_smooth(method = "lm")

qplot(stra, Points, data = Students2014) + geom_smooth(method = "lm")

qplot(surf, Points, data = Students2014) + geom_smooth(method = "lm")

Next some more illustration of the effects of the other variables to the ‘points’-variable and summaries of these

  1. In the following simple regression models Points is used as the target variable while other variables are used as explanatory variables. There is also a summary provided for all of the individual regressions.
M_Attitude <- lm(Points ~ Attitude, data = Students2014)
summary(M_Attitude)
## 
## Call:
## lm(formula = Points ~ Attitude, data = Students2014)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -16.9763  -3.2119   0.4339   4.1534  10.6645 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 11.63715    1.83035   6.358 1.95e-09 ***
## Attitude     0.35255    0.05674   6.214 4.12e-09 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 5.32 on 164 degrees of freedom
## Multiple R-squared:  0.1906, Adjusted R-squared:  0.1856 
## F-statistic: 38.61 on 1 and 164 DF,  p-value: 4.119e-09
M_Age <- lm(Points ~ Age, data = Students2014)
summary(M_Age)
## 
## Call:
## lm(formula = Points ~ Age, data = Students2014)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -16.0360  -3.7531   0.0958   4.6762  10.8128 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 24.52150    1.57339  15.585   <2e-16 ***
## Age         -0.07074    0.05901  -1.199    0.232    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 5.887 on 164 degrees of freedom
## Multiple R-squared:  0.008684,   Adjusted R-squared:  0.00264 
## F-statistic: 1.437 on 1 and 164 DF,  p-value: 0.2324
M_gender <- lm(Points ~ gender, data = Students2014)
summary(M_gender)
## 
## Call:
## lm(formula = Points ~ gender, data = Students2014)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -15.3273  -3.3273   0.5179   4.5179  10.6727 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  22.3273     0.5613  39.776   <2e-16 ***
## genderM       1.1549     0.9664   1.195    0.234    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 5.887 on 164 degrees of freedom
## Multiple R-squared:  0.008632,   Adjusted R-squared:  0.002587 
## F-statistic: 1.428 on 1 and 164 DF,  p-value: 0.2338
M_deep <- lm(Points ~ deep, data = Students2014)
summary(M_deep)
## 
## Call:
## lm(formula = Points ~ deep, data = Students2014)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -15.6913  -3.6935   0.2862   4.9957  10.3537 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  23.1141     3.0908   7.478 4.31e-12 ***
## deep         -0.1080     0.8306  -0.130    0.897    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 5.913 on 164 degrees of freedom
## Multiple R-squared:  0.000103,   Adjusted R-squared:  -0.005994 
## F-statistic: 0.01689 on 1 and 164 DF,  p-value: 0.8967
M_stra <- lm(Points ~ stra, data = Students2014)
summary(M_stra)
## 
## Call:
## lm(formula = Points ~ stra, data = Students2014)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -16.5581  -3.8198   0.1042   4.3024  10.1394 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   19.233      1.897  10.141   <2e-16 ***
## stra           1.116      0.590   1.892   0.0603 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 5.849 on 164 degrees of freedom
## Multiple R-squared:  0.02135,    Adjusted R-squared:  0.01538 
## F-statistic: 3.578 on 1 and 164 DF,  p-value: 0.06031
M_surf <- lm(Points ~ surf, data = Students2014)
summary(M_surf)
## 
## Call:
## lm(formula = Points ~ surf, data = Students2014)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -14.6539  -3.3744   0.3574   4.4734  10.2234 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  27.2017     2.4432  11.134   <2e-16 ***
## surf         -1.6091     0.8613  -1.868   0.0635 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 5.851 on 164 degrees of freedom
## Multiple R-squared:  0.02084,    Adjusted R-squared:  0.01487 
## F-statistic:  3.49 on 1 and 164 DF,  p-value: 0.06351

Looking at the output of the summaries, the most trustworthy explanatory variables are Attitude, Age, surf and stra. These are confirmed by the scatterplots of the individual dependences of points on the other variables. Age, Attitude and stra can be seen to probably have influence on points. The values of the regression make these assumption reasonable, with a quite low margin for failure. (Failure in this case means selecting output or in other words a random sample where the assumptions fail to reflect or descibe the truth/dependence of the variables).

The mean, standard error and variance for the dataset’s variables:

sapply(Students2014, mean, na.rm = TRUE)
## Warning in mean.default(X[[i]], ...): argument is not numeric or logical:
## returning NA
##    gender       Age  Attitude      deep      stra      surf    Points 
##        NA 25.512048 31.427711  3.679719  3.121235  2.787149 22.716867
sapply(Students2014, sd, na.rm = TRUE)
## Warning in var(if (is.vector(x) || is.factor(x)) x else as.double(x), na.rm = na.rm): Calling var(x) on a factor x is deprecated and will become an error.
##   Use something like 'all(duplicated(x)[-1L])' to test for a constant vector.
##    gender       Age  Attitude      deep      stra      surf    Points 
## 0.4742358 7.7660785 7.2990794 0.5541369 0.7718318 0.5288405 5.8948836
sapply(Students2014, var, na.rm = TRUE)
## Warning in FUN(X[[i]], ...): Calling var(x) on a factor x is deprecated and will become an error.
##   Use something like 'all(duplicated(x)[-1L])' to test for a constant vector.
##     gender        Age   Attitude       deep       stra       surf 
##  0.2248996 60.3119752 53.2765608  0.3070677  0.5957244  0.2796722 
##     Points 
## 34.7496532

Below is a regression model where exam points is the target/dependent variable, with three explanatory variables. These explanatory variables were chosen because it can be seen that they correlate with the variable that we are attempting to explain (points). At the same time the model is drawn in several different ways to help interpret and understand it’s relevance.

Model3 <- lm(formula = Points ~ Attitude + Age + stra, data = Students2014)
plot(Model3)

The following functions work in my R-project, but for some reason they refused to knit to HTML. I have taken measures to enable the knitting of error terms etc, and it still won’t work. (I used these functions to make some interesting plots that I discuss later in the exercise. These were not individually necessary for ch2, and I have provided a collection of the mentioned plots in an other way also. This collection is included in the code). Still, I wanted to post them here to show what I did in another way. Readers please note: These were not as such demanded, and are provided in the necessary form later. If experimentation is done on them, I recommend copying them to an R-document, along with all the other necessary elements

r.squared(Model3, model = NULL, type = c(“Attitude”, “Age”, “stra”), dfcor = TRUE) #Normal Q-Q r.squared(Model3, model = NULL, type = c(“Attitude”, “Age”, “stra”), dfcor = FALSE) r.squared(Model3, model = “lm”, type = c(“Attitude”, “Age”, “stra”), dfcor = TRUE) #Res vs Lev r.squared(Model3, model = “lm”, type = c(“Attitude”, “Age”, “stra”), dfcor = FALSE) #Res vs Fit

  1. Finally, a summary of the model:
summary(Model3)
## 
## Call:
## lm(formula = Points ~ Attitude + Age + stra, data = Students2014)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -18.1149  -3.2003   0.3303   3.4129  10.7599 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 10.89543    2.64834   4.114 6.17e-05 ***
## Attitude     0.34808    0.05622   6.191 4.72e-09 ***
## Age         -0.08822    0.05302  -1.664   0.0981 .  
## stra         1.00371    0.53434   1.878   0.0621 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 5.26 on 162 degrees of freedom
## Multiple R-squared:  0.2182, Adjusted R-squared:  0.2037 
## F-statistic: 15.07 on 3 and 162 DF,  p-value: 1.07e-08

The summary provides seemingly significant results, with considerably high correlation to points of all the other variables. The error has remained quite low in relation to the amount of variables used. The r-squared shows generally how close the data on the variables is to the regression line. Experimenting with different inputs to the function r-squared 4 different but interesting outputs can be found with this ampunt of experimentation. It seems that the Multiple R^2 is quite low, so it’s not too significant (It doesn’t deny the validity of the regression. The variants of the r-squared functions produced the Residuals vs Fitted, Residuals vs Leverage, and Normal Q-Q plots, as well as Scale location.

  1. Next, three diagnostic plots are combined to help consider the validity of the model
par(mfrow = c(2,2))
plot(Model3, which = c(1,2,5))

Linear regression models have a few general assumtions: 1. Linearity 2. The errors of the model are normally distributed. 2. The errors are not correlated. 3. The sizes of the errors do not depend on the variables used to explain the target variable.

Now let’s think how the plots produced correspond (or not) to these assumption:

The Q–Q-plot demonstrates how the standardised residuals of the model fit to the theory or reasoning behind the model. Therefore the normal distribution assumption seems to be true for the model.

The residuals vs. fitted values -plot does not seem to be regular/subjected to any pattern, meaning that the errors are not correlated to the explanatory variables and their size is independent.

Therefore all of the assumptions are valid for the model created.


Chapter 3 - Logistic Regression

There was a problem reading the file into R from my computer; The code worked but the data didn’t print out correctly, even though the same exact code combinations worked elsewhere, so I have included the necessary parts of the wrangling part to create the dataset again.

cache=F
library(tidyr)
library(dplyr)
library(ggplot2)
library(gmodels)
student_mat <-  read.csv("~/Documents/IODS-project/data/student-mat.csv", sep = ";")
student_por <- read.csv("~/Documents/IODS-project/data/student-por.csv", sep = ";")

join_by <- c("school","sex","age","address","famsize","Pstatus","Medu","Fedu","Mjob","Fjob","reason","nursery","internet")

math_por <- inner_join(student_mat, student_por, by = join_by, suffix = c(".student_mat", ".student_por"))

alc <- select(math_por, one_of(join_by))

notjoined_columns <- colnames(student_mat)[!colnames(student_mat) %in% join_by]


# for every column name not used for joining...
for(column_name in notjoined_columns) {
  # select two columns from 'math_por' with the same original name
  two_columns <- select(math_por, starts_with(column_name))
  # select the first column vector of those two columns
  first_column <- select(two_columns, 1)[[1]]
  
  # if that first column vector is numeric...
  if(is.numeric(first_column)) {
    # take a rounded average of each row of the two columns and
    # add the resulting vector to the alc data frame
    alc[column_name] <- round(rowMeans(two_columns))
  } else { # else if it's not numeric...
    # add the first column vector to the alc data frame
    alc[column_name] <- first_column
  }
}


alc <- mutate(alc, alc_use = (Dalc + Walc) / 2)
alc <- mutate(alc, high_use = alc_use > 2)
  1. Let’s read the alc data. (This works but the original output wasn’t maintained from when I first wrote the code, as I explained)
#alc <- read.csv("~/Documents/IODS-project/data/create_alc.R", row.names = NULL)
names(alc)
##  [1] "school"     "sex"        "age"        "address"    "famsize"   
##  [6] "Pstatus"    "Medu"       "Fedu"       "Mjob"       "Fjob"      
## [11] "reason"     "nursery"    "internet"   "guardian"   "traveltime"
## [16] "studytime"  "failures"   "schoolsup"  "famsup"     "paid"      
## [21] "activities" "higher"     "romantic"   "famrel"     "freetime"  
## [26] "goout"      "Dalc"       "Walc"       "health"     "absences"  
## [31] "G1"         "G2"         "G3"         "alc_use"    "high_use"
  1. For my variables, I chose sex, final exam scores, famsize and studytime. My hypotheses are as follows: Fewer females than males who took part consume a lot of alcohol. I would also assume that a person who doesn’t consume a lot of alcohol gets better grades than someone who drinks more.

  2. Now we must test the hypothesis.

alc %>% group_by(sex, high_use) %>%summarise(count=n(),mean_grade=mean(G3))
## # A tibble: 4 x 4
## # Groups:   sex [?]
##   sex   high_use count mean_grade
##   <fct> <lgl>    <int>      <dbl>
## 1 F     FALSE      156       11.4
## 2 F     TRUE        42       11.7
## 3 M     FALSE      112       12.2
## 4 M     TRUE        72       10.3

First we have the crosstabulation for alcohol usage, sex and the average of the final exam grades. The results of this tabulation tell us two things: i.) A higher percentage of men than women use alcohol a lot. ii.) Interestingly, those women who drink more have slightly higher grades. In contrast, binge-drinking men have worse grades than those who don’t drink a lot.

Then let’s create a boxplot of high alcohol usages effects to the final grades.

g1 <- ggplot(alc, aes(x= high_use, y=G3), col=sex)
g1 + geom_boxplot()+ylab("grade")

It is clear that on average, alcohol-lovers have worse grades than moderate drinkers.

Then we have a barplot describing the relationship; alcohol usage vs. study time. Those who do not drink much are included in the darkred bar and those who drink a lot in the darkblue bar.

counts <- table(alc$high_use, alc$studytime)
barplot(counts, main = "Usage of alcohol", xlab = "studytime", col = c("darkred", "darkblue"), beside = TRUE)

The key ratio described is the share of the drinkaholics in each category of study time. The shares are about equally small in 4 and 3 but get bigger in 2 and in 1 it is almost as common to drink a lot than not to drink a lot.

Then we compare alcohol usage to family size.

counts2 <- table(alc$high_use, alc$famsize)
barplot(counts2, main = "Usage of alcohol", xlab = "famsize", col = c("black", "gold"), beside = TRUE)

The result is that students from small families, are somewhat more likely to drink a lot than those from big families.

  1. The logistic regression describing our varibles follows.
m <- glm(high_use ~ famsize + sex + G3 + studytime, data = alc, family = "binomial")
summary(m)
## 
## Call:
## glm(formula = high_use ~ famsize + sex + G3 + studytime, family = "binomial", 
##     data = alc)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -1.4556  -0.8448  -0.6728   1.1674   2.1425  
## 
## Coefficients:
##             Estimate Std. Error z value Pr(>|z|)   
## (Intercept)  0.36140    0.50827   0.711  0.47706   
## famsizeLE3   0.31601    0.25722   1.229  0.21923   
## sexM         0.67284    0.24445   2.752  0.00592 **
## G3          -0.07463    0.03598  -2.074  0.03806 * 
## studytime   -0.41808    0.15933  -2.624  0.00869 **
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 465.68  on 381  degrees of freedom
## Residual deviance: 435.28  on 377  degrees of freedom
## AIC: 445.28
## 
## Number of Fisher Scoring iterations: 4

Coefficients as odds ratios:

OR <- coef(m) %>% exp
OR
## (Intercept)  famsizeLE3        sexM          G3   studytime 
##   1.4353404   1.3716456   1.9598015   0.9280889   0.6583124

COnfidence intervals:

CI <- confint(m) %>% exp
## Waiting for profiling to be done...
CI
##                 2.5 %    97.5 %
## (Intercept) 0.5317356 3.9213022
## famsizeLE3  0.8244681 2.2647911
## sexM        1.2165115 3.1774177
## G3          0.8641265 0.9954148
## studytime   0.4773416 0.8930376
  1. Sex ja study time seem to have a statistical relationship to alcohol use. That’s why we modify the model to include only sex and study time.
m <- glm(high_use ~ sex + studytime, data = alc, family = "binomial")
probabilities <- predict(m, type = "response")
alc <- mutate(alc, probability = probabilities)
alc <- mutate(alc, prediction = probability > 0.3)

select(alc, studytime, sex, high_use, probability, prediction) %>% tail(10)
##     studytime sex high_use probability prediction
## 373         1   M    FALSE   0.4795667       TRUE
## 374         1   M     TRUE   0.4795667       TRUE
## 375         3   F    FALSE   0.1536065      FALSE
## 376         1   F    FALSE   0.3222140       TRUE
## 377         3   F    FALSE   0.1536065      FALSE
## 378         2   F    FALSE   0.2270396      FALSE
## 379         2   F    FALSE   0.2270396      FALSE
## 380         2   F    FALSE   0.2270396      FALSE
## 381         1   M     TRUE   0.4795667       TRUE
## 382         1   M     TRUE   0.4795667       TRUE
table(high_use = alc$high_use, prediction = alc$prediction)
##         prediction
## high_use FALSE TRUE
##    FALSE   158  110
##    TRUE     37   77
CrossTable(alc$high_use, alc$prediction)
## 
##  
##    Cell Contents
## |-------------------------|
## |                       N |
## | Chi-square contribution |
## |           N / Row Total |
## |           N / Col Total |
## |         N / Table Total |
## |-------------------------|
## 
##  
## Total Observations in Table:  382 
## 
##  
##              | alc$prediction 
## alc$high_use |     FALSE |      TRUE | Row Total | 
## -------------|-----------|-----------|-----------|
##        FALSE |       158 |       110 |       268 | 
##              |     3.283 |     3.424 |           | 
##              |     0.590 |     0.410 |     0.702 | 
##              |     0.810 |     0.588 |           | 
##              |     0.414 |     0.288 |           | 
## -------------|-----------|-----------|-----------|
##         TRUE |        37 |        77 |       114 | 
##              |     7.719 |     8.049 |           | 
##              |     0.325 |     0.675 |     0.298 | 
##              |     0.190 |     0.412 |           | 
##              |     0.097 |     0.202 |           | 
## -------------|-----------|-----------|-----------|
## Column Total |       195 |       187 |       382 | 
##              |     0.510 |     0.490 |           | 
## -------------|-----------|-----------|-----------|
## 
## 
counts3 <- table(alc$high_use, alc$prediction)
barplot(counts3, main = "Usage of alcohol", xlab = "high_use", col = c("pink", "purple"), beside = TRUE)

g <- ggplot(alc, aes(x = probability, y = high_use, col = prediction))

g + geom_point()

table(high_use = alc$high_use, prediction = alc$prediction) %>% prop.table %>% addmargins
##         prediction
## high_use      FALSE       TRUE        Sum
##    FALSE 0.41361257 0.28795812 0.70157068
##    TRUE  0.09685864 0.20157068 0.29842932
##    Sum   0.51047120 0.48952880 1.00000000
loss_func <- function(class, prob) {
  n_wrong <- abs(class - prob) > 0.5
  mean(n_wrong)
}

loss_func(class = alc$high_use, prob = 0)
## [1] 0.2984293

According to the results, the model provided provides a sense of reality, even if it it statistically not very reliable. By simply guessing what the results would be, the ideas that one comes up with could be similar but more accurate. What the model probably works for though, is that is offers some slight validation for any guesses made. Not much can be concluded from the model alone however, since the ratio of wrong predictions is quite large.

  1. Here is ten-fold cross-validation of the model produced earlier. The average prediction error is higher than the one on datacamp. Let us see if one with a lower error can be found in Q8.
library(boot)
## 
## Attaching package: 'boot'
## The following object is masked from 'package:lattice':
## 
##     melanoma
cv <- cv.glm(data = alc, cost = loss_func, glmfit = m, K = 10)
cv$delta[1]
## [1] 0.3115183
  1. Let us make and compare some different models. First there is a model that is slightly larger than the original one. Second, there is one with a lot more predictors. Next a smaller model with different variables than the original m.
n <- glm(high_use ~ sex + studytime + famsize + failures + activities + absences, data = alc, family = "binomial")
probabilities <- predict(n, type = "response")

select(alc, studytime, sex, high_use, probability, prediction) %>% tail(10)
##     studytime sex high_use probability prediction
## 373         1   M    FALSE   0.4795667       TRUE
## 374         1   M     TRUE   0.4795667       TRUE
## 375         3   F    FALSE   0.1536065      FALSE
## 376         1   F    FALSE   0.3222140       TRUE
## 377         3   F    FALSE   0.1536065      FALSE
## 378         2   F    FALSE   0.2270396      FALSE
## 379         2   F    FALSE   0.2270396      FALSE
## 380         2   F    FALSE   0.2270396      FALSE
## 381         1   M     TRUE   0.4795667       TRUE
## 382         1   M     TRUE   0.4795667       TRUE
table(high_use = alc$high_use, prediction = alc$prediction)
##         prediction
## high_use FALSE TRUE
##    FALSE   158  110
##    TRUE     37   77
CrossTable(alc$high_use, alc$prediction)
## 
##  
##    Cell Contents
## |-------------------------|
## |                       N |
## | Chi-square contribution |
## |           N / Row Total |
## |           N / Col Total |
## |         N / Table Total |
## |-------------------------|
## 
##  
## Total Observations in Table:  382 
## 
##  
##              | alc$prediction 
## alc$high_use |     FALSE |      TRUE | Row Total | 
## -------------|-----------|-----------|-----------|
##        FALSE |       158 |       110 |       268 | 
##              |     3.283 |     3.424 |           | 
##              |     0.590 |     0.410 |     0.702 | 
##              |     0.810 |     0.588 |           | 
##              |     0.414 |     0.288 |           | 
## -------------|-----------|-----------|-----------|
##         TRUE |        37 |        77 |       114 | 
##              |     7.719 |     8.049 |           | 
##              |     0.325 |     0.675 |     0.298 | 
##              |     0.190 |     0.412 |           | 
##              |     0.097 |     0.202 |           | 
## -------------|-----------|-----------|-----------|
## Column Total |       195 |       187 |       382 | 
##              |     0.510 |     0.490 |           | 
## -------------|-----------|-----------|-----------|
## 
## 
g <- ggplot(alc, aes(x = probability, y = high_use, col = prediction))

g + geom_point()

table(high_use = alc$high_use, prediction = alc$prediction) %>% prop.table %>% addmargins
##         prediction
## high_use      FALSE       TRUE        Sum
##    FALSE 0.41361257 0.28795812 0.70157068
##    TRUE  0.09685864 0.20157068 0.29842932
##    Sum   0.51047120 0.48952880 1.00000000
loss_func <- function(class, prob) {
  n_wrong <- abs(class - prob) > 0.5
  mean(n_wrong)
}

loss_func(class = alc$high_use, prob = 0)
## [1] 0.2984293
cv <- cv.glm(data = alc, cost = loss_func, glmfit = m, K = 10)
cv$delta[1]
## [1] 0.3062827
o <- glm(high_use ~ sex + studytime + famsize + failures + activities + absences + health + goout + freetime + romantic, data = alc, family = "binomial")
probabilities <- predict(o, type = "response")

select(alc, studytime, sex, high_use, probability, prediction) %>% tail(10)
##     studytime sex high_use probability prediction
## 373         1   M    FALSE   0.4795667       TRUE
## 374         1   M     TRUE   0.4795667       TRUE
## 375         3   F    FALSE   0.1536065      FALSE
## 376         1   F    FALSE   0.3222140       TRUE
## 377         3   F    FALSE   0.1536065      FALSE
## 378         2   F    FALSE   0.2270396      FALSE
## 379         2   F    FALSE   0.2270396      FALSE
## 380         2   F    FALSE   0.2270396      FALSE
## 381         1   M     TRUE   0.4795667       TRUE
## 382         1   M     TRUE   0.4795667       TRUE
table(high_use = alc$high_use, prediction = alc$prediction)
##         prediction
## high_use FALSE TRUE
##    FALSE   158  110
##    TRUE     37   77
CrossTable(alc$high_use, alc$prediction)
## 
##  
##    Cell Contents
## |-------------------------|
## |                       N |
## | Chi-square contribution |
## |           N / Row Total |
## |           N / Col Total |
## |         N / Table Total |
## |-------------------------|
## 
##  
## Total Observations in Table:  382 
## 
##  
##              | alc$prediction 
## alc$high_use |     FALSE |      TRUE | Row Total | 
## -------------|-----------|-----------|-----------|
##        FALSE |       158 |       110 |       268 | 
##              |     3.283 |     3.424 |           | 
##              |     0.590 |     0.410 |     0.702 | 
##              |     0.810 |     0.588 |           | 
##              |     0.414 |     0.288 |           | 
## -------------|-----------|-----------|-----------|
##         TRUE |        37 |        77 |       114 | 
##              |     7.719 |     8.049 |           | 
##              |     0.325 |     0.675 |     0.298 | 
##              |     0.190 |     0.412 |           | 
##              |     0.097 |     0.202 |           | 
## -------------|-----------|-----------|-----------|
## Column Total |       195 |       187 |       382 | 
##              |     0.510 |     0.490 |           | 
## -------------|-----------|-----------|-----------|
## 
## 
g <- ggplot(alc, aes(x = probability, y = high_use, col = prediction))

g + geom_point()

table(high_use = alc$high_use, prediction = alc$prediction) %>% prop.table %>% addmargins
##         prediction
## high_use      FALSE       TRUE        Sum
##    FALSE 0.41361257 0.28795812 0.70157068
##    TRUE  0.09685864 0.20157068 0.29842932
##    Sum   0.51047120 0.48952880 1.00000000
loss_func <- function(class, prob) {
  n_wrong <- abs(class - prob) > 0.5
  mean(n_wrong)
}

loss_func(class = alc$high_use, prob = 0)
## [1] 0.2984293
cv <- cv.glm(data = alc, cost = loss_func, glmfit = m, K = 10)
cv$delta[1]
## [1] 0.2984293
p <- glm(high_use ~ failures + activities + absences + health + goout + freetime + romantic, data = alc, family = "binomial")
probabilities <- predict(p, type = "response")

select(alc, studytime, sex, high_use, probability, prediction) %>% tail(10)
##     studytime sex high_use probability prediction
## 373         1   M    FALSE   0.4795667       TRUE
## 374         1   M     TRUE   0.4795667       TRUE
## 375         3   F    FALSE   0.1536065      FALSE
## 376         1   F    FALSE   0.3222140       TRUE
## 377         3   F    FALSE   0.1536065      FALSE
## 378         2   F    FALSE   0.2270396      FALSE
## 379         2   F    FALSE   0.2270396      FALSE
## 380         2   F    FALSE   0.2270396      FALSE
## 381         1   M     TRUE   0.4795667       TRUE
## 382         1   M     TRUE   0.4795667       TRUE
table(high_use = alc$high_use, prediction = alc$prediction)
##         prediction
## high_use FALSE TRUE
##    FALSE   158  110
##    TRUE     37   77
CrossTable(alc$high_use, alc$prediction)
## 
##  
##    Cell Contents
## |-------------------------|
## |                       N |
## | Chi-square contribution |
## |           N / Row Total |
## |           N / Col Total |
## |         N / Table Total |
## |-------------------------|
## 
##  
## Total Observations in Table:  382 
## 
##  
##              | alc$prediction 
## alc$high_use |     FALSE |      TRUE | Row Total | 
## -------------|-----------|-----------|-----------|
##        FALSE |       158 |       110 |       268 | 
##              |     3.283 |     3.424 |           | 
##              |     0.590 |     0.410 |     0.702 | 
##              |     0.810 |     0.588 |           | 
##              |     0.414 |     0.288 |           | 
## -------------|-----------|-----------|-----------|
##         TRUE |        37 |        77 |       114 | 
##              |     7.719 |     8.049 |           | 
##              |     0.325 |     0.675 |     0.298 | 
##              |     0.190 |     0.412 |           | 
##              |     0.097 |     0.202 |           | 
## -------------|-----------|-----------|-----------|
## Column Total |       195 |       187 |       382 | 
##              |     0.510 |     0.490 |           | 
## -------------|-----------|-----------|-----------|
## 
## 
g <- ggplot(alc, aes(x = probability, y = high_use, col = prediction))

g + geom_point()

table(high_use = alc$high_use, prediction = alc$prediction) %>% prop.table %>% addmargins
##         prediction
## high_use      FALSE       TRUE        Sum
##    FALSE 0.41361257 0.28795812 0.70157068
##    TRUE  0.09685864 0.20157068 0.29842932
##    Sum   0.51047120 0.48952880 1.00000000
loss_func <- function(class, prob) {
  n_wrong <- abs(class - prob) > 0.5
  mean(n_wrong)
}

loss_func(class = alc$high_use, prob = 0)
## [1] 0.2984293
cv <- cv.glm(data = alc, cost = loss_func, glmfit = m, K = 10)
cv$delta[1]
## [1] 0.2984293

As one could expect, increasing the amount of predictors decreases the average error, assuming the predictions from the smaller model are maintained. Choosing a few optimal predictors should yield a smaller number than choosing a large number of not-so-good predictors. None of the models I created produced a smaller average error than the one on datacamp, and I would guess it to be quite difficult to find such a model. What surprised me was the amount by which the average prediction error changes between the models produced. I expected it to variate more.


summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Chapter 4 - Clustering and classification

First we load the “Boston” data from MASS package and explore it.

library(MASS)
## 
## Attaching package: 'MASS'
## The following object is masked from 'package:dplyr':
## 
##     select
data("Boston")
str(Boston)
## 'data.frame':    506 obs. of  14 variables:
##  $ crim   : num  0.00632 0.02731 0.02729 0.03237 0.06905 ...
##  $ zn     : num  18 0 0 0 0 0 12.5 12.5 12.5 12.5 ...
##  $ indus  : num  2.31 7.07 7.07 2.18 2.18 2.18 7.87 7.87 7.87 7.87 ...
##  $ chas   : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ nox    : num  0.538 0.469 0.469 0.458 0.458 0.458 0.524 0.524 0.524 0.524 ...
##  $ rm     : num  6.58 6.42 7.18 7 7.15 ...
##  $ age    : num  65.2 78.9 61.1 45.8 54.2 58.7 66.6 96.1 100 85.9 ...
##  $ dis    : num  4.09 4.97 4.97 6.06 6.06 ...
##  $ rad    : int  1 2 2 3 3 3 5 5 5 5 ...
##  $ tax    : num  296 242 242 222 222 222 311 311 311 311 ...
##  $ ptratio: num  15.3 17.8 17.8 18.7 18.7 18.7 15.2 15.2 15.2 15.2 ...
##  $ black  : num  397 397 393 395 397 ...
##  $ lstat  : num  4.98 9.14 4.03 2.94 5.33 ...
##  $ medv   : num  24 21.6 34.7 33.4 36.2 28.7 22.9 27.1 16.5 18.9 ...
summary(Boston)
##       crim                zn             indus            chas        
##  Min.   : 0.00632   Min.   :  0.00   Min.   : 0.46   Min.   :0.00000  
##  1st Qu.: 0.08204   1st Qu.:  0.00   1st Qu.: 5.19   1st Qu.:0.00000  
##  Median : 0.25651   Median :  0.00   Median : 9.69   Median :0.00000  
##  Mean   : 3.61352   Mean   : 11.36   Mean   :11.14   Mean   :0.06917  
##  3rd Qu.: 3.67708   3rd Qu.: 12.50   3rd Qu.:18.10   3rd Qu.:0.00000  
##  Max.   :88.97620   Max.   :100.00   Max.   :27.74   Max.   :1.00000  
##       nox               rm             age              dis        
##  Min.   :0.3850   Min.   :3.561   Min.   :  2.90   Min.   : 1.130  
##  1st Qu.:0.4490   1st Qu.:5.886   1st Qu.: 45.02   1st Qu.: 2.100  
##  Median :0.5380   Median :6.208   Median : 77.50   Median : 3.207  
##  Mean   :0.5547   Mean   :6.285   Mean   : 68.57   Mean   : 3.795  
##  3rd Qu.:0.6240   3rd Qu.:6.623   3rd Qu.: 94.08   3rd Qu.: 5.188  
##  Max.   :0.8710   Max.   :8.780   Max.   :100.00   Max.   :12.127  
##       rad              tax           ptratio          black       
##  Min.   : 1.000   Min.   :187.0   Min.   :12.60   Min.   :  0.32  
##  1st Qu.: 4.000   1st Qu.:279.0   1st Qu.:17.40   1st Qu.:375.38  
##  Median : 5.000   Median :330.0   Median :19.05   Median :391.44  
##  Mean   : 9.549   Mean   :408.2   Mean   :18.46   Mean   :356.67  
##  3rd Qu.:24.000   3rd Qu.:666.0   3rd Qu.:20.20   3rd Qu.:396.23  
##  Max.   :24.000   Max.   :711.0   Max.   :22.00   Max.   :396.90  
##      lstat            medv      
##  Min.   : 1.73   Min.   : 5.00  
##  1st Qu.: 6.95   1st Qu.:17.02  
##  Median :11.36   Median :21.20  
##  Mean   :12.65   Mean   :22.53  
##  3rd Qu.:16.95   3rd Qu.:25.00  
##  Max.   :37.97   Max.   :50.00
names(Boston)
##  [1] "crim"    "zn"      "indus"   "chas"    "nox"     "rm"      "age"    
##  [8] "dis"     "rad"     "tax"     "ptratio" "black"   "lstat"   "medv"

The “Boston”-data is comprised of 506 observations of 14 different variables. The “Names()”-function provides the names of the variables included. The function “Summary()” produces the distributions of each variable.

Now we explore the relationship between variables.

library(corrplot)
## corrplot 0.84 loaded
library(tidyverse)
## ── Attaching packages ───────────────────────────────────────────────────────────────────────── tidyverse 1.2.1 ──
## ✔ tibble  1.4.2     ✔ purrr   0.2.5
## ✔ readr   1.2.1     ✔ stringr 1.3.1
## ✔ tibble  1.4.2     ✔ forcats 0.3.0
## ── Conflicts ──────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ✖ MASS::select()  masks dplyr::select()
cor_matrix<-cor(Boston)  %>% round(digits=2)
corrplot(cor_matrix, type="upper", cl.pos = "b",tl.pos = "d",tl.cex = 0.6  )

In the graph, the blue colour represents positive correlation between variables while the red rpresents the negatives. The darker the colour and the bigger the ball, the stronger the correlation is. For example, the big blue ball between “rad” and “tax” tells that there is a strong correlation between access to highways and property tax rate.

Now we’ll standardise the data

boston_scaled <- scale(Boston)
summary(boston_scaled)
##       crim                 zn               indus        
##  Min.   :-0.419367   Min.   :-0.48724   Min.   :-1.5563  
##  1st Qu.:-0.410563   1st Qu.:-0.48724   1st Qu.:-0.8668  
##  Median :-0.390280   Median :-0.48724   Median :-0.2109  
##  Mean   : 0.000000   Mean   : 0.00000   Mean   : 0.0000  
##  3rd Qu.: 0.007389   3rd Qu.: 0.04872   3rd Qu.: 1.0150  
##  Max.   : 9.924110   Max.   : 3.80047   Max.   : 2.4202  
##       chas              nox                rm               age         
##  Min.   :-0.2723   Min.   :-1.4644   Min.   :-3.8764   Min.   :-2.3331  
##  1st Qu.:-0.2723   1st Qu.:-0.9121   1st Qu.:-0.5681   1st Qu.:-0.8366  
##  Median :-0.2723   Median :-0.1441   Median :-0.1084   Median : 0.3171  
##  Mean   : 0.0000   Mean   : 0.0000   Mean   : 0.0000   Mean   : 0.0000  
##  3rd Qu.:-0.2723   3rd Qu.: 0.5981   3rd Qu.: 0.4823   3rd Qu.: 0.9059  
##  Max.   : 3.6648   Max.   : 2.7296   Max.   : 3.5515   Max.   : 1.1164  
##       dis               rad               tax             ptratio       
##  Min.   :-1.2658   Min.   :-0.9819   Min.   :-1.3127   Min.   :-2.7047  
##  1st Qu.:-0.8049   1st Qu.:-0.6373   1st Qu.:-0.7668   1st Qu.:-0.4876  
##  Median :-0.2790   Median :-0.5225   Median :-0.4642   Median : 0.2746  
##  Mean   : 0.0000   Mean   : 0.0000   Mean   : 0.0000   Mean   : 0.0000  
##  3rd Qu.: 0.6617   3rd Qu.: 1.6596   3rd Qu.: 1.5294   3rd Qu.: 0.8058  
##  Max.   : 3.9566   Max.   : 1.6596   Max.   : 1.7964   Max.   : 1.6372  
##      black             lstat              medv        
##  Min.   :-3.9033   Min.   :-1.5296   Min.   :-1.9063  
##  1st Qu.: 0.2049   1st Qu.:-0.7986   1st Qu.:-0.5989  
##  Median : 0.3808   Median :-0.1811   Median :-0.1449  
##  Mean   : 0.0000   Mean   : 0.0000   Mean   : 0.0000  
##  3rd Qu.: 0.4332   3rd Qu.: 0.6024   3rd Qu.: 0.2683  
##  Max.   : 0.4406   Max.   : 3.5453   Max.   : 2.9865

Now all the observations are using the same scale. Making assumptions about the summary for example is meaningful.

Then we must turn “boston_scaled” back into a data.frame

boston_scaled <- as.data.frame(boston_scaled)

These lines give us the cathegorical variable “crime”

bins <- quantile(boston_scaled$crim)
crime <- cut(boston_scaled$crim, breaks = bins, include.lowest = TRUE, labels = c("low", "med_low", "med_high", "high"))

And these replace the old variable “crim” with the cathegorical crime

boston_scaled <- dplyr::select(boston_scaled, -crim)
boston_scaled <- data.frame(boston_scaled, crime)

Then we divide the data to train with 80% of the data and test with 20% of the data.

n <- nrow(boston_scaled)
ind <- sample(n,  size = n * 0.8)
train <- boston_scaled[ind,]
test <- boston_scaled[-ind,]
  1. Because the data has now been cathegorised into separate sets for training and testing, we can use the training-part for linear discriminant analysis, where crime rate will be predicted by all of the other variables.
lda.fit <- lda(crime ~ ., data = train)
lda.arrows <- function(x, myscale = 1, arrow_heads = 0.1, color = "red", tex = 0.75, choices = c(1,2)){
  heads <- coef(x)
  arrows(x0 = 0, y0 = 0, 
         x1 = myscale * heads[,choices[1]], 
         y1 = myscale * heads[,choices[2]], col=color, length = arrow_heads)
  text(myscale * heads[,choices], labels = row.names(heads), 
       cex = tex, col=color, pos=3)
}

classes <- as.numeric(train$crime)

plot(lda.fit, dimen = 2, col=classes)
lda.arrows(lda.fit, myscale = 2)

Looking at the bi-plot, it’s clear that the “rad”-variable acts (on its own) as a predictor of “high crime rate” in the Boston data. The other 12 variables are associated with low, medium low and medium high rates of crime. The grouping based on these 12 variables is quite vague and it is difficult to see whether any of the variables can accurately/adequately sort the associated observations.

crime_cat<-test$crime
test<-dplyr::select(test, -crime)
lda.pred<-predict(lda.fit, newdata = test)
table(correct = crime_cat, predicted = lda.pred$class)
##           predicted
## correct    low med_low med_high high
##   low       14      10        1    0
##   med_low    5      17        6    0
##   med_high   0       6       13    1
##   high       0       0        0   29

The amount of correct cases and of predicted cases for each of the cathegories (low, med_low, med_high, high) varies between every sample matrix. The change is to be expected as the sets we have created have been randomly classified. There is much less variation between predictions for the “high” class than there is in the predictions of the others.

  1. Before we practice K-means clustering, we should reload the Boston data, scale it, and afterwards measure the distances between the observations.
data(Boston)
boston_scaled1<-as.data.frame(scale(Boston))
dist_eu<-dist(boston_scaled1)
summary(dist_eu)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##  0.1343  3.4625  4.8241  4.9111  6.1863 14.3970
head(boston_scaled1)
##         crim         zn      indus       chas        nox        rm
## 1 -0.4193669  0.2845483 -1.2866362 -0.2723291 -0.1440749 0.4132629
## 2 -0.4169267 -0.4872402 -0.5927944 -0.2723291 -0.7395304 0.1940824
## 3 -0.4169290 -0.4872402 -0.5927944 -0.2723291 -0.7395304 1.2814456
## 4 -0.4163384 -0.4872402 -1.3055857 -0.2723291 -0.8344581 1.0152978
## 5 -0.4120741 -0.4872402 -1.3055857 -0.2723291 -0.8344581 1.2273620
## 6 -0.4166314 -0.4872402 -1.3055857 -0.2723291 -0.8344581 0.2068916
##          age      dis        rad        tax    ptratio     black
## 1 -0.1198948 0.140075 -0.9818712 -0.6659492 -1.4575580 0.4406159
## 2  0.3668034 0.556609 -0.8670245 -0.9863534 -0.3027945 0.4406159
## 3 -0.2655490 0.556609 -0.8670245 -0.9863534 -0.3027945 0.3960351
## 4 -0.8090878 1.076671 -0.7521778 -1.1050216  0.1129203 0.4157514
## 5 -0.5106743 1.076671 -0.7521778 -1.1050216  0.1129203 0.4406159
## 6 -0.3508100 1.076671 -0.7521778 -1.1050216  0.1129203 0.4101651
##        lstat       medv
## 1 -1.0744990  0.1595278
## 2 -0.4919525 -0.1014239
## 3 -1.2075324  1.3229375
## 4 -1.3601708  1.1815886
## 5 -1.0254866  1.4860323
## 6 -1.0422909  0.6705582

The scaled Boston data will now be used for K-means clustering. It isn’t trivial (in many cases) to investigate on the number of clusters that can classify the data. Therefore, we need to first randomize the usage of a certain number of clusters.

First let’s start with a random number cluster. Let us choose k=4 and apply k-means on the data.

kmm = kmeans(boston_scaled1,6,nstart = 50 ,iter.max = 15) 

The elbow method is one good technique using which we can estimate the number of clusters.

library(ggplot2)
set.seed(1234)
k.max <- 15
data <- boston_scaled1
wss <- sapply(1:k.max, 
              function(k){kmeans(data, k)$tot.withinss})
qplot(1:k.max, wss, geom = c("point", "line"), span = 0.2,
     xlab="Number of clusters K",
     ylab="Total within-clusters sum of squares")
## Warning: Ignoring unknown parameters: span

## Warning: Ignoring unknown parameters: span

The elbow plot seems to indicate that we may not find more than two clear clusters but it’s good to confirm predictions using another method because there is no shortage of methods for analyses like these. Let’s try the “NbClust”- package.

library(NbClust)
nb <- NbClust(boston_scaled1, diss=NULL, distance = "euclidean", 
              min.nc=2, max.nc=5, method = "kmeans", 
              index = "all", alphaBeale = 0.1)

## *** : The Hubert index is a graphical method of determining the number of clusters.
##                 In the plot of Hubert index, we seek a significant knee that corresponds to a 
##                 significant increase of the value of the measure i.e the significant peak in Hubert
##                 index second differences plot. 
## 

## *** : The D index is a graphical method of determining the number of clusters. 
##                 In the plot of D index, we seek a significant knee (the significant peak in Dindex
##                 second differences plot) that corresponds to a significant increase of the value of
##                 the measure. 
##  
## ******************************************************************* 
## * Among all indices:                                                
## * 12 proposed 2 as the best number of clusters 
## * 6 proposed 3 as the best number of clusters 
## * 3 proposed 4 as the best number of clusters 
## * 3 proposed 5 as the best number of clusters 
## 
##                    ***** Conclusion *****                            
##  
## * According to the majority rule, the best number of clusters is  2 
##  
##  
## *******************************************************************
hist(nb$Best.nc[1,], breaks = max(na.omit(nb$Best.nc[1,])))

Now, it’s easier to see that the data is described better with two clusters. With that, we should run the k-means algorithm again.

km_final = kmeans(boston_scaled1, centers = 2) 
pairs(boston_scaled1[3:9], col=km_final$cluster)

The clusters in the above plot are divided into two groups and outlined using the colors red and black. Some of the pairs are better grouped than other ones in the plot. One of the important observations can be made with the “chas”-variable where the observations in all of the pairs formed by it are wrongly clustered. Still, clusters formed by the “rad” variable are better separated.

Bonus:

Now, we will use a randomly selected cluster number (k=6) and perform LDA. We shall follow the the basic steps of scaling and distance calculation. Afterwards, we will find out how the biplot looks (of the whole data set) as we try to group it into six different categories.

boston_scaled2<-as.data.frame(scale(Boston))
head(boston_scaled2)
##         crim         zn      indus       chas        nox        rm
## 1 -0.4193669  0.2845483 -1.2866362 -0.2723291 -0.1440749 0.4132629
## 2 -0.4169267 -0.4872402 -0.5927944 -0.2723291 -0.7395304 0.1940824
## 3 -0.4169290 -0.4872402 -0.5927944 -0.2723291 -0.7395304 1.2814456
## 4 -0.4163384 -0.4872402 -1.3055857 -0.2723291 -0.8344581 1.0152978
## 5 -0.4120741 -0.4872402 -1.3055857 -0.2723291 -0.8344581 1.2273620
## 6 -0.4166314 -0.4872402 -1.3055857 -0.2723291 -0.8344581 0.2068916
##          age      dis        rad        tax    ptratio     black
## 1 -0.1198948 0.140075 -0.9818712 -0.6659492 -1.4575580 0.4406159
## 2  0.3668034 0.556609 -0.8670245 -0.9863534 -0.3027945 0.4406159
## 3 -0.2655490 0.556609 -0.8670245 -0.9863534 -0.3027945 0.3960351
## 4 -0.8090878 1.076671 -0.7521778 -1.1050216  0.1129203 0.4157514
## 5 -0.5106743 1.076671 -0.7521778 -1.1050216  0.1129203 0.4406159
## 6 -0.3508100 1.076671 -0.7521778 -1.1050216  0.1129203 0.4101651
##        lstat       medv
## 1 -1.0744990  0.1595278
## 2 -0.4919525 -0.1014239
## 3 -1.2075324  1.3229375
## 4 -1.3601708  1.1815886
## 5 -1.0254866  1.4860323
## 6 -1.0422909  0.6705582
set.seed(1234)
km_bs2<-kmeans(dist_eu, centers = 6)
head(km_bs2)
## $cluster
##   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18 
##   6   6   6   6   6   6   6   4   4   6   4   6   6   4   4   4   6   4 
##  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36 
##   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4 
##  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54 
##   4   6   6   1   1   6   6   6   6   6   6   4   4   6   6   6   6   6 
##  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72 
##   1   1   1   1   6   6   6   6   6   6   1   1   1   6   6   6   6   6 
##  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90 
##   6   6   6   6   4   4   4   6   6   6   6   6   6   6   6   6   6   6 
##  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107 108 
##   6   6   6   6   4   6   6   3   3   6   4   4   4   4   4   4   4   4 
## 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 
##   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   2   4   4 
## 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 
##   2   2   2   2   4   4   4   4   2   2   2   2   2   2   2   2   5   2 
## 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 
##   5   5   5   5   5   2   2   2   5   2   5   5   5   3   4   2   3   3 
## 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 
##   3   3   4   4   3   4   4   4   4   4   4   4   4   6   6   6   6   6 
## 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 
##   3   6   6   6   4   6   3   6   6   1   1   1   1   1   1   1   1   1 
## 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 
##   1   1   1   1   1   1   1   6   6   4   3   3   3   3   3   6   4   6 
## 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 
##   3   4   3   3   3   3   3   6   3   3   3   6   3   6   4   6   3   3 
## 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 
##   3   4   3   6   6   6   6   6   6   6   6   6   6   6   6   6   6   6 
## 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 
##   1   1   1   1   1   3   3   3   3   3   3   3   3   3   3   3   3   3 
## 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 
##   6   6   6   3   3   6   3   3   6   6   3   6   3   1   1   1   1   6 
## 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 
##   6   1   1   1   1   6   6   6   6   6   1   1   1   6   6   6   6   6 
## 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 
##   6   6   4   4   4   4   4   4   4   4   4   4   4   4   6   6   6   4 
## 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 
##   6   6   6   6   6   6   6   6   6   6   6   6   6   6   6   6   6   1 
## 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 
##   6   6   1   6   6   1   1   1   6   1   1   1   1   1   5   5   5   2 
## 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 
##   2   2   2   5   5   5   2   5   5   5   5   2   5   5   5   2   2   2 
## 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 
##   2   2   5   2   2   2   5   5   5   5   5   2   2   2   2   2   2   2 
## 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 
##   2   2   5   2   5   2   2   2   5   5   5   2   2   2   5   5   5   5 
## 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 
##   5   5   5   5   5   5   2   2   2   5   5   5   5   5   5   5   2   5 
## 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 
##   2   2   5   5   5   5   5   2   2   2   2   2   2   5   2   2   2   2 
## 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 
##   5   2   2   2   5   5   5   5   2   2   2   2   2   2   2   2   5   2 
## 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 
##   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2 
## 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 
##   2   2   2   2   5   2   2   4   4   4   4   4   4   4   4   4   4   4 
## 505 506 
##   4   4 
## 
## $centers
##          1        2        3        4        5        6        7        8
## 1 3.989644 4.556297 4.216580 3.977768 4.072917 4.089124 4.248097 4.850842
## 2 5.635860 5.015922 5.810840 6.243394 6.219170 5.797678 5.007134 5.045384
## 3 4.115845 4.367908 3.914214 4.415556 4.277226 4.521520 4.462950 4.591271
## 4 3.322873 2.419949 3.555546 3.901515 3.917887 3.259432 2.872770 3.145128
## 5 7.347551 6.917018 7.668887 8.029178 8.008552 7.594726 6.746593 6.719663
## 6 2.552963 2.191615 2.547985 2.481257 2.636215 2.126035 2.463163 3.260455
##          9       10       11       12       13       14       15       16
## 1 6.023239 4.730761 5.085827 4.546510 4.256435 5.022876 5.346690 5.064489
## 2 5.275376 5.034209 5.024306 5.008656 5.191566 4.483294 4.239085 4.500312
## 3 5.861940 4.897185 5.053174 4.724166 4.806196 5.041859 5.037049 5.123212
## 4 3.802664 3.078288 3.240106 2.958259 3.171899 2.294340 2.083575 2.330854
## 5 6.637733 6.672022 6.656885 6.725987 6.831291 6.522547 6.271019 6.514117
## 6 4.420281 3.050288 3.460627 2.800817 2.548810 2.498158 2.847417 2.519525
##         17       18       19       20       21       22       23       24
## 1 4.839017 5.494447 5.410418 5.409972 6.301722 5.559564 5.849309 6.119225
## 2 4.932293 4.118743 4.699219 4.231080 4.140866 4.107180 4.064205 4.123917
## 3 5.188031 5.159236 5.556072 5.185846 5.777782 5.080391 5.333884 5.590056
## 4 2.926372 2.006490 2.829410 2.073005 2.478201 1.996680 2.200197 2.363123
## 5 6.848068 6.136584 6.246617 6.243108 5.944153 6.154788 6.070741 6.047729
## 6 2.477710 2.972866 3.035484 2.815474 3.883785 3.047242 3.413583 3.706860
##         25       26       27       28       29       30       31       32
## 1 5.776453 5.909337 5.645826 5.792679 5.463721 5.291108 6.251560 5.790675
## 2 4.125236 4.229138 4.173627 4.154204 4.209655 4.253839 4.190000 4.132815
## 3 5.355804 5.648491 5.331440 5.423799 4.956368 4.757484 5.818922 5.290357
## 4 2.152358 2.399737 2.130704 2.313981 2.194705 2.226566 2.563711 2.185395
## 5 6.124494 5.850285 6.119300 5.848617 6.281408 6.323214 5.926407 6.111869
## 6 3.317906 3.492059 3.174430 3.407128 3.072595 2.917289 3.883866 3.341829
##         33       34       35       36       37       38       39       40
## 1 6.577978 6.113220 6.286376 5.003424 4.975828 4.672432 4.567581 2.437628
## 2 4.543962 4.083659 4.244460 4.504907 4.504214 4.851253 4.995628 6.988766
## 3 6.168152 5.623677 5.717537 4.728770 4.767845 4.847078 4.787796 5.456166
## 4 3.295133 2.297075 2.760000 2.036929 2.052171 2.484306 2.720323 5.019539
## 5 5.702765 5.899302 5.635547 6.472198 6.389736 6.751702 6.860251 8.586641
## 6 4.437132 3.655982 4.014459 2.356225 2.324457 2.085109 2.100542 3.469146
##         41       42       43       44       45       46       47       48
## 1 2.550075 4.003727 4.101822 4.105741 4.205545 4.493890 4.535762 5.127133
## 2 7.347068 6.266063 6.066725 6.010008 5.405380 5.401641 5.323536 4.969192
## 3 5.469978 4.947438 5.088076 5.079073 4.810066 5.080046 5.057556 5.071546
## 4 5.447668 4.089165 3.798403 3.739937 2.899376 2.938610 2.889613 2.590240
## 5 8.944151 7.985202 7.757987 7.727039 7.185569 7.146905 7.056687 6.715830
## 6 3.806323 2.464106 2.277093 2.245886 1.890630 2.124631 2.187826 2.870058
##         49       50       51       52       53       54       55       56
## 1 6.354068 4.788056 3.687296 3.635669 3.127248 3.365371 3.768251 2.573205
## 2 5.425549 5.185207 5.660587 5.607665 6.251113 6.093472 6.228695 8.172561
## 3 6.206363 5.152123 5.037840 4.798345 4.875844 5.098446 6.488456 6.248718
## 4 3.649819 2.729138 3.311553 3.194530 3.999725 3.806354 4.738612 6.360064
## 5 6.857506 6.903606 7.327416 7.333311 7.954340 7.750576 7.846534 9.656599
## 6 4.374890 2.484987 2.216591 2.155992 2.202966 2.192392 4.011314 4.620562
##         57       58       59       60       61       62       63       64
## 1 2.435993 2.586150 3.455264 3.762957 4.163099 4.634886 3.692547 3.384146
## 2 7.625711 8.169946 5.852530 5.357849 5.193164 4.988228 5.388941 5.749506
## 3 6.314081 6.255593 5.396948 5.291164 5.423722 5.411219 4.967901 5.080093
## 4 5.728217 6.380166 3.850185 3.257986 3.128914 3.007457 3.306577 3.782189
## 5 9.093653 9.562610 7.619436 7.182935 6.981514 6.757826 7.282673 7.580278
## 6 4.138053 4.788808 2.369128 2.238707 2.577586 3.001430 2.348247 2.423646
##         65       66       67       68       69       70       71       72
## 1 3.572872 2.365488 2.854471 3.828722 4.245479 3.861492 4.311560 4.482338
## 2 6.803815 7.202476 6.871459 5.702754 5.382884 5.506994 5.679697 5.332385
## 3 5.070095 5.920134 6.075955 5.331567 5.513971 5.232351 5.172823 5.258275
## 4 4.571372 5.363377 5.039822 3.540862 3.214330 3.286807 3.670384 3.233668
## 5 8.474576 8.706790 8.325184 7.481800 7.133931 7.317062 7.509722 7.139492
## 6 3.099810 3.755717 3.762213 2.164752 2.377335 2.067120 2.395258 2.279879
##         73       74       75       76       77       78       79       80
## 1 4.423047 4.374894 4.565665 4.571969 4.993178 4.715682 4.645174 4.703818
## 2 5.670343 5.628241 5.253844 4.504533 4.080734 4.392771 4.369389 4.613917
## 3 5.331200 5.256931 5.138078 4.739219 4.676271 4.796607 4.781211 5.037161
## 4 3.633211 3.612120 3.532216 2.554765 2.115305 2.435091 2.406743 2.698489
## 5 7.496987 7.426016 7.172186 6.550720 6.164051 6.445026 6.421550 6.621032
## 6 2.419650 2.395619 2.582255 2.208724 2.569263 2.284706 2.281898 2.322846
##         81       82       83       84       85       86       87       88
## 1 3.228722 3.647245 3.352955 3.510806 4.214393 4.216222 4.470996 4.539526
## 2 5.833680 5.345002 5.671433 5.439196 5.298921 5.378578 5.181960 5.119600
## 3 4.611003 4.507704 4.824876 4.799211 4.493640 4.250144 4.736784 4.543074
## 4 3.593223 2.978716 3.362220 3.049922 2.723815 2.845202 2.603436 2.477017
## 5 7.679009 7.247432 7.495607 7.269305 7.155841 7.257393 6.986917 6.981295
## 6 2.077143 2.128207 1.941076 1.906614 1.839655 1.960338 2.004413 2.011357
##         89       90       91       92       93       94       95       96
## 1 4.787782 4.431266 4.656816 4.754404 4.105175 4.001644 4.521147 4.347592
## 2 5.260876 5.460377 5.023131 4.997348 4.772459 5.169144 4.479851 5.437460
## 3 4.086432 3.906040 4.252949 4.267811 4.510455 4.766874 4.611235 4.143402
## 4 2.894806 3.107514 2.443156 2.419531 2.751041 3.222217 2.448424 2.937590
## 5 7.155386 7.332941 6.878036 6.862533 6.774331 7.083584 6.505216 7.188899
## 6 2.659893 2.401307 2.216247 2.322228 2.415933 2.468939 2.704529 2.182013
##         97       98       99      100      101      102      103      104
## 1 4.715695 4.926015 4.640776 4.444450 5.282563 5.152315 6.572203 5.683531
## 2 5.051112 6.412041 6.764467 5.837829 4.205067 4.305317 5.036975 3.815398
## 3 4.508034 3.931800 4.158120 3.883013 4.489959 4.495810 6.000236 5.001289
## 4 2.400338 4.464342 4.810546 3.603211 2.414290 2.493991 3.966604 1.972705
## 5 6.868421 8.203118 8.491487 7.681807 6.393702 6.492260 5.811685 5.987192
## 6 2.261612 3.692387 3.630574 2.739459 2.966666 2.843788 4.692793 3.172095
##        105      106      107      108      109      110      111      112
## 1 5.724543 6.103378 6.104999 5.770964 5.777602 5.809233 5.260548 5.174233
## 2 3.840428 3.789013 3.785737 3.801714 3.887880 3.778476 4.058712 3.895295
## 3 4.956142 5.253274 5.307506 4.990050 4.894350 5.027813 4.918935 4.183520
## 4 2.022137 2.164155 2.196416 2.023648 2.172750 2.042266 2.184470 2.238546
## 5 6.014221 5.881611 5.856170 5.944714 6.084653 5.930743 6.185964 6.069764
## 6 3.223448 3.618628 3.631351 3.259679 3.351457 3.335422 2.743048 2.868558
##        113      114      115      116      117      118      119      120
## 1 5.795991 5.778504 5.457332 5.719642 5.202323 5.373780 5.497713 5.357061
## 2 3.601847 3.596459 3.722199 3.590667 3.748787 3.744213 3.671311 3.776235
## 3 4.819193 4.774286 4.530960 4.818453 4.451377 4.589012 4.741708 4.798190
## 4 2.075218 2.106269 2.052459 2.053031 1.971584 1.988655 2.048517 2.045794
## 5 5.667421 5.680606 5.851170 5.493919 5.878062 5.869090 5.544023 5.809181
## 6 3.375466 3.392521 3.026294 3.301736 2.747953 2.915253 3.063432 2.869590
##        121      122      123      124      125      126      127      128
## 1 6.426088 6.565457 6.806695 7.305465 6.897705 6.650747 7.533959 7.099802
## 2 4.261019 4.162980 4.128457 4.247333 4.123875 4.163650 4.355189 3.161437
## 3 5.427653 5.424854 5.565025 6.069215 5.659762 5.427403 6.344535 5.999576
## 4 2.985164 2.938368 3.039074 3.475730 3.062302 2.973639 3.677551 2.962007
## 5 6.237519 6.139760 6.064278 6.001360 6.051491 6.152869 5.974765 5.445330
## 6 4.116775 4.252645 4.510005 5.058229 4.583383 4.343081 5.287399 4.735059
##        129      130      131      132      133      134      135      136
## 1 6.875876 7.154803 6.726119 6.686918 6.596530 6.821369 7.166802 6.870993
## 2 3.183589 3.174651 3.265601 3.234532 3.348169 3.162883 3.338913 3.134950
## 3 5.627423 6.120486 5.509731 5.516882 5.382470 5.779404 6.139554 5.674601
## 4 2.931268 3.021401 2.885217 2.835481 2.871725 2.806058 3.188140 2.903821
## 5 5.604137 5.425648 5.688097 5.652880 5.721190 5.497188 5.035538 5.535608
## 6 4.590233 4.791289 4.456575 4.402201 4.350142 4.479053 4.894039 4.582035
##        137      138      139      140      141      142      143      144
## 1 6.929533 6.859261 7.327005 7.036231 7.324311 8.387851 9.118028 8.148386
## 2 3.098547 3.195797 3.203702 3.121999 3.270140 4.203114 6.057048 4.491078
## 3 5.819167 5.632765 6.197877 5.809014 6.167568 7.327389 6.736697 6.601594
## 4 2.859060 2.928458 3.172007 2.969872 3.289011 4.421385 6.210476 4.719098
## 5 5.406565 5.610438 5.411571 5.483828 5.434774 5.732297 6.519423 5.705948
## 6 4.585322 4.572210 4.994596 4.721431 5.047669 6.196553 7.427033 6.200054
##        145      146      147      148      149      150      151      152
## 1 8.591160 8.420611 8.051529 8.485530 8.220414 7.812856 7.329573 7.603008
## 2 4.881773 4.956580 4.786268 4.850486 4.658853 4.298230 4.279168 4.390872
## 3 7.209721 6.881696 6.562636 7.091389 6.756783 6.281936 5.599987 6.029305
## 4 5.120965 5.221320 4.864032 5.058645 4.839584 4.425501 4.201583 4.325365
## 5 5.882920 5.395652 5.350043 5.860027 5.668932 5.539694 5.844264 5.733672
## 6 6.628290 6.634143 6.205886 6.539074 6.293195 5.851136 5.439102 5.651936
##        153      154      155      156      157      158      159      160
## 1 8.622664 7.648725 8.381752 8.889137 8.425203 6.168678 6.265700 7.165808
## 2 6.067192 4.403229 5.843835 6.536516 5.328492 5.218020 4.320602 4.558378
## 3 6.369979 6.052779 5.844207 6.703763 7.158351 4.234387 4.756091 5.368031
## 4 5.967163 4.417624 5.805631 6.566625 5.400744 4.276378 3.305864 4.351927
## 5 6.541819 5.446370 6.344801 6.334474 5.503366 6.942094 6.074535 6.178603
## 6 6.952748 5.752673 6.793409 7.450863 6.650053 4.652464 4.273222 5.383270
##        161      162      163      164      165      166      167      168
## 1 7.231434 6.383671 7.606315 7.711715 6.150603 6.283786 6.542030 6.239898
## 2 5.940257 6.088162 7.394089 7.646620 4.086328 4.360674 6.228342 4.356613
## 3 4.605274 4.398804 4.791045 4.937649 4.851661 4.926498 4.456582 5.095670
## 4 5.239834 5.208814 6.712875 6.998166 3.062315 3.452702 5.427142 3.432231
## 5 6.821006 7.745103 8.432676 8.695086 5.936568 5.663492 7.854149 5.576630
## 6 5.752473 5.237762 6.745173 6.961257 4.080738 4.385762 5.475612 4.315636
##        169      170      171      172      173      174      175      176
## 1 6.213385 6.133807 6.433082 6.312035 5.454412 4.962647 4.941506 4.309193
## 2 4.170734 4.100748 4.054129 4.025673 4.699883 4.808037 4.816591 5.496225
## 3 4.728033 4.677209 5.202442 5.015219 4.676351 4.091021 4.407110 4.153032
## 4 3.234454 3.142836 3.181914 3.077576 2.488721 2.512347 2.447970 3.326034
## 5 5.730715 5.794348 5.516106 5.715890 6.426784 6.668081 6.607389 7.275149
## 6 4.254128 4.150282 4.374444 4.230784 3.104965 2.654763 2.534686 2.342553
##        177      178      179      180      181      182      183      184
## 1 4.520962 4.655885 4.649899 4.604653 5.132343 4.814462 5.087352 5.054530
## 2 5.015596 4.976181 5.157347 5.945635 6.198433 5.587189 5.931557 5.543818
## 3 4.411335 4.095254 3.737891 3.879091 3.800673 4.190561 3.860567 4.061937
## 4 2.658053 2.616342 2.975519 3.642452 4.178165 3.143454 3.744498 3.144853
## 5 6.792977 6.830833 7.010189 7.720425 7.949502 7.318332 7.703218 7.337983
## 6 2.175426 2.365924 2.589714 2.810682 3.656914 2.701373 3.342620 2.979474
##        185      186      187      188      189      190      191      192
## 1 5.360158 4.777155 5.085672 3.049814 2.837691 2.892236 2.592917 2.487691
## 2 5.146074 5.258702 6.977902 6.090983 6.253786 6.421824 6.840347 6.509686
## 3 4.706625 4.276810 4.103340 4.369622 4.665779 4.360736 4.840840 4.814221
## 4 2.617485 2.667473 5.069928 4.220068 4.355866 4.608931 5.027524 4.620020
## 5 6.823279 6.988966 8.637738 7.764729 7.876360 8.097468 8.409214 8.135143
## 6 3.009695 2.461779 4.108449 2.947084 2.866184 3.171421 3.299804 2.939362
##        193      194      195       196      197      198      199      200
## 1 2.626002 2.323019 2.285881  3.495329 2.818135 2.825122 2.840680 2.451539
## 2 6.916387 7.350478 7.173909  8.592641 8.185358 7.919777 8.103044 7.965488
## 3 4.786382 5.315615 5.265629  5.625695 5.786297 5.784978 5.720384 6.255528
## 4 5.115810 5.261122 5.038340  6.858470 6.362933 6.100498 6.288002 6.311681
## 5 8.533158 8.873489 8.672208 10.009282 9.580192 9.208468 9.481059 9.444290
## 6 3.385297 3.423873 3.258558  5.286849 4.731058 4.582926 4.715512 4.639590
##        201      202      203       204       205      206      207
## 1 2.456624 2.615509 2.868195  3.538131  3.737246 4.677119 4.523642
## 2 7.969849 7.156198 8.271244  8.640337  8.822374 4.966944 4.612421
## 3 6.272606 5.771920 5.739854  5.855113  5.957838 4.945805 4.407545
## 4 6.329487 5.273529 6.554190  6.965429  7.174721 2.820461 2.266758
## 5 9.430213 8.587107 9.723775 10.042167 10.216692 6.837520 6.613955
## 6 4.653597 3.907488 4.866644  5.491799  5.693985 2.252863 1.996460
##        208      209      210      211      212      213      214      215
## 1 5.173003 6.173648 7.246427 6.672900 7.168488 6.358530 4.424781 5.994199
## 2 4.338029 5.951508 5.923358 5.785418 5.899356 5.931043 4.957469 5.447417
## 3 4.839209 4.297639 5.214301 4.486356 5.179802 4.522080 4.441468 6.128544
## 4 2.088170 4.501917 4.748828 4.456045 4.719108 4.521710 2.774437 3.942573
## 5 6.238923 7.056415 6.886396 6.882747 6.859678 7.017952 6.877368 6.804923
## 6 2.684155 4.559083 5.472929 4.942984 5.391736 4.669469 2.088834 4.075614
##        216      217      218      219      220      221      222      223
## 1 4.533967 6.461391 5.090497 6.968673 6.593520 6.320911 6.799719 6.130295
## 2 4.734048 5.887167 4.502200 5.707295 5.831456 6.068344 5.809474 6.083430
## 3 4.493721 4.333352 3.927803 4.474244 4.098632 3.855927 4.507648 3.793452
## 4 2.431159 4.642940 2.604000 4.619602 4.623502 4.781149 4.654611 4.752233
## 5 6.701716 6.923664 6.481639 6.712071 6.932306 7.216588 6.814993 7.226735
## 6 2.008161 4.833935 2.937343 5.253528 4.990749 4.858718 5.140070 4.702050
##        224      225      226      227      228      229      230      231
## 1 4.738585 5.386965 5.891695 5.165618 4.738937 4.794451 4.323631 4.757661
## 2 4.706017 6.297753 6.888318 5.848384 4.984604 6.583434 5.480585 4.442431
## 3 3.859178 3.856074 4.287951 3.761622 3.704484 4.327344 4.344291 4.345763
## 4 2.632171 4.857703 5.604564 4.291528 3.086868 5.025870 3.556647 2.180368
## 5 6.676587 8.061548 8.562013 7.691942 6.882629 8.267586 7.279603 6.313200
## 6 2.510992 4.285144 5.053107 3.807224 2.766869 3.866941 2.455648 2.274652
##        232      233      234      235      236      237      238      239
## 1 4.653852 5.135020 5.281054 5.952219 4.649091 6.018095 4.490512 2.932965
## 2 5.189298 6.314557 6.527581 6.182940 4.494536 6.049817 5.234441 6.100275
## 3 3.705792 3.899503 3.957570 3.819634 4.306913 3.896708 3.743105 4.916380
## 4 3.349510 4.826196 5.081229 4.795798 2.237307 4.663958 3.356024 4.006857
## 5 7.086045 8.097837 8.247294 7.256051 6.371138 7.184499 7.164135 7.763565
## 6 2.845373 4.094662 4.348568 4.582063 2.164414 4.571565 2.701412 2.261099
##        240      241      242      243      244      245      246      247
## 1 2.956836 3.179208 3.559831 3.174838 2.972611 4.361124 4.499702 3.449697
## 2 5.782692 5.644636 5.418724 5.676824 6.431679 5.293119 5.282827 5.730382
## 3 4.609977 4.554218 4.836874 4.812866 5.251961 5.595467 5.698876 5.306870
## 4 3.612081 3.523994 3.199172 3.505714 4.402069 3.307803 3.374542 3.724642
## 5 7.513168 7.401734 7.139957 7.340494 8.034419 6.985702 6.964082 7.498675
## 6 2.096391 2.294939 2.317889 2.233775 2.546385 2.843542 3.010876 2.298688
##        248      249      250      251      252      253      254      255
## 1 3.980918 3.426770 3.241080 3.321813 3.366958 3.347543 4.117622 2.565061
## 2 5.332551 5.535635 6.098106 6.038428 6.187201 6.732703 7.679236 7.519106
## 3 5.197006 5.027748 5.230422 5.313640 5.406880 5.560045 5.671848 6.347835
## 4 3.315715 3.504649 4.188133 4.105072 4.276291 4.932310 6.124355 5.596288
## 5 7.123772 7.327359 7.884487 7.824608 7.913195 8.440374 9.349847 8.962012
## 6 2.612063 2.227342 2.472500 2.394111 2.529107 3.107181 4.463447 4.036629
##        256      257      258      259      260      261      262      263
## 1 2.791995 2.848603 6.222798 5.518648 5.417650 5.174118 5.463188 5.971533
## 2 7.592589 8.192506 7.613422 6.131531 5.811603 5.956088 6.518475 7.320962
## 3 6.580192 5.707147 4.551395 3.894518 4.041152 3.826233 3.886648 4.317743
## 4 5.699971 6.403033 6.371304 4.637079 4.211517 4.374564 5.047654 6.017951
## 5 8.992176 9.653615 9.041679 7.629278 7.342798 7.487956 8.005527 8.762238
## 6 4.155292 4.824381 5.880265 4.531389 4.220084 4.147002 4.710711 5.556397
##        264      265      266      267      268      269      270      271
## 1 5.408871 5.364474 5.369503 5.311210 5.468341 4.684862 5.853082 4.111760
## 2 5.873985 6.084410 5.582297 5.691716 7.365150 6.850545 6.292985 5.143047
## 3 3.944671 3.834643 4.846831 4.038079 4.202225 3.938496 4.496815 4.877938
## 4 4.361095 4.543509 3.870860 4.139572 5.912661 5.158425 4.630825 2.685788
## 5 7.399951 7.595295 6.985984 7.167083 8.815364 8.370001 7.374525 6.914144
## 6 4.321892 4.369594 3.895689 4.141040 5.180260 4.218897 4.554789 2.091791
##        272      273      274      275      276      277      278      279
## 1 3.725629 3.921894 5.523364 4.973350 3.058289 4.960332 4.861229 3.070627
## 2 5.736385 5.155769 7.186009 7.238343 6.006291 7.213299 7.355393 5.795132
## 3 4.839362 4.286203 4.148237 4.380952 4.296659 4.210140 4.469765 4.503523
## 4 3.441028 2.665134 5.602045 5.598971 3.877658 5.612713 5.710319 3.612360
## 5 7.520064 7.064234 8.378912 8.386111 7.804129 8.364865 8.493915 7.564951
## 6 2.111547 1.993339 4.916068 4.705346 2.513014 4.779926 4.711343 2.285977
##        280      281      282      283       284      285      286      287
## 1 3.561388 4.216680 3.374750 5.571630  5.408034 2.345212 2.477254 2.927409
## 2 6.491419 7.105591 6.595419 8.219563  9.973467 7.928601 6.930874 7.445534
## 3 4.173615 3.975881 4.184062 4.440886  6.268257 5.936035 5.428512 6.501695
## 4 4.285849 5.167816 4.389736 6.563865  8.344298 6.072569 4.772330 5.471836
## 5 8.122015 8.703650 8.222715 9.258337 10.933893 9.363760 8.450626 8.767961
## 6 2.790991 3.947935 2.795306 5.515391  6.980733 4.422708 3.123714 4.079753
##        288      289      290      291      292      293      294      295
## 1 2.509960 2.585977 2.400932 2.683501 2.813661 2.686863 4.345410 4.479945
## 2 6.413629 6.249971 6.494620 6.930359 7.229010 6.875832 5.592884 5.181463
## 3 5.343188 5.207865 5.287981 5.569837 5.473392 5.650164 4.941731 4.796956
## 4 4.396113 4.214046 4.535171 5.074359 5.432490 5.006978 3.576391 3.058148
## 5 8.041222 7.905277 8.061694 8.582296 8.865102 8.509272 7.358111 6.995131
## 6 2.756566 2.716768 2.843575 3.668080 3.989047 3.608840 2.497209 2.387338
##        296      297      298      299      300      301      302      303
## 1 4.101880 4.215355 4.808009 2.456182 2.336450 2.394242 3.068423 2.941845
## 2 5.702683 5.379947 5.078750 7.315906 7.652372 7.121431 5.575441 5.929574
## 3 4.531097 4.405035 5.064135 5.938158 5.803548 5.507286 4.604003 4.762883
## 4 3.690081 3.281272 3.028002 5.478237 5.872746 5.284133 3.549854 3.969692
## 5 7.527753 7.235927 6.829083 8.722048 9.087048 8.634313 7.337638 7.605560
## 6 2.491650 2.393691 2.750925 3.788908 4.039227 3.734424 2.232083 2.376030
##        304      305      306      307      308      309      310      311
## 1 2.855273 3.436586 3.684046 3.978741 3.854584 4.998677 5.185967 5.719675
## 2 6.336183 6.106407 5.435975 5.842022 5.453779 4.466812 4.127252 4.624290
## 3 4.561650 4.196815 4.266337 4.017975 4.168573 4.202224 4.535816 5.594347
## 4 4.456508 4.073418 3.192729 3.845712 3.261918 2.344549 1.843049 2.847242
## 5 8.039455 7.868780 7.241481 7.658684 7.290961 6.546441 6.168368 6.187983
## 6 2.734380 2.774880 2.361712 3.039054 2.564437 2.631756 2.582365 3.293284
##        312      313      314      315      316      317      318      319
## 1 4.937184 5.460141 5.098426 5.028976 5.293601 5.460000 5.215271 4.838880
## 2 4.467670 4.023815 4.217072 4.259199 4.159874 4.029901 4.115912 4.245101
## 3 4.476328 4.594927 4.348331 4.178642 4.891978 4.889172 4.794276 4.262780
## 4 2.297145 1.850451 1.966400 2.079641 1.938234 1.959365 1.906122 1.969043
## 5 6.476538 6.069256 6.284457 6.344720 6.136907 5.966724 6.078177 6.307673
## 6 2.402724 2.904430 2.574041 2.606905 2.711944 2.970980 2.658070 2.293294
##        320      321      322      323      324      325      326      327
## 1 4.842685 4.449705 4.484974 4.585730 5.057642 4.334264 4.248227 4.277525
## 2 4.240360 4.799353 4.776401 4.757420 4.457601 5.018766 5.526219 5.218292
## 3 4.534455 4.506705 4.537503 4.811214 4.996492 4.590902 4.987239 4.873621
## 4 1.963279 2.473559 2.434921 2.406058 2.072533 2.775066 3.465771 3.042737
## 5 6.250142 6.816256 6.795014 6.731231 6.397846 6.994614 7.395759 7.139104
## 6 2.254385 1.961575 1.975895 2.005309 2.474246 1.958008 2.220200 2.005009
##        328      329      330      331      332      333      334      335
## 1 4.511237 4.336792 4.068778 4.134704 3.513382 3.127529 4.341878 4.382338
## 2 4.796609 5.372181 5.613018 5.389720 5.942590 6.090114 5.485716 5.424178
## 3 4.877258 5.105806 4.900165 4.957900 5.488342 5.279997 5.053555 5.093833
## 4 2.517471 3.305539 3.594198 3.303857 3.752625 3.884973 3.226203 3.160665
## 5 6.725423 7.068932 7.323299 7.087469 7.538669 7.661878 7.331467 7.267954
## 6 2.035908 2.350131 2.312741 2.230762 2.580536 2.403384 2.205917 2.203234
##        336      337      338      339      340      341      342      343
## 1 4.485929 4.729718 4.812037 4.629130 4.744764 4.837628 2.808348 4.362098
## 2 5.347455 5.032342 4.935566 5.124323 4.979263 4.865181 6.823411 5.448631
## 3 5.156767 5.112746 5.110110 5.016265 5.044939 4.987777 4.514441 4.784059
## 4 3.056405 2.647838 2.524309 2.778949 2.590159 2.417256 4.633530 3.331511
## 5 7.189428 6.898354 6.809633 7.000683 6.865025 6.777057 8.446366 7.165257
## 6 2.186727 2.223765 2.322145 2.157595 2.205404 2.271625 3.007511 2.617582
##        344      345      346      347      348      349      350      351
## 1 2.778316 2.366030 4.320697 4.466973 2.400876 2.213242 2.925825 3.077473
## 2 5.684615 6.353501 5.565696 5.487424 7.183736 7.319965 6.755452 6.526367
## 3 4.757585 4.927327 5.421207 5.492314 6.193135 5.934386 5.524385 5.618626
## 4 3.802233 4.548197 3.369890 3.310384 5.476084 5.437402 4.699873 4.404974
## 5 7.461637 8.044999 7.305607 7.150579 8.715944 8.820537 8.437225 8.219455
## 6 2.672462 2.940953 2.423474 2.543259 3.956148 3.799867 3.066109 2.914978
##        352      353       354      355      356      357      358      359
## 1 2.847020 3.254646  3.220675 4.018615 3.810904 8.663013 8.315674 8.225028
## 2 7.254755 7.319860  8.653858 7.691074 7.769223 4.621450 4.715344 4.749805
## 3 6.269580 6.763444  7.013329 7.521812 7.391746 6.500890 6.156007 6.172788
## 4 5.498901 5.585389  6.831393 6.079486 6.140023 5.907878 5.797393 5.786448
## 5 8.766898 8.789968 10.015464 9.117773 9.216143 5.456756 5.735313 5.758167
## 6 3.882617 4.035485  5.152518 4.846552 4.789845 6.995105 6.715094 6.630405
##        360      361      362      363      364      365      366      367
## 1 7.278021 7.215827 7.464845 7.699553 8.598542 8.542707 8.530898 7.799054
## 2 2.467536 2.805217 2.333627 2.769367 4.652258 6.207836 4.624219 2.863464
## 3 6.182375 6.025704 6.270247 6.585418 6.526351 6.398780 7.725367 6.760694
## 4 4.207340 4.344110 4.232657 4.378729 5.817697 6.961379 5.534146 4.394301
## 5 4.964540 5.192113 4.710339 4.977082 5.468166 7.258598 5.920246 4.657637
## 6 5.340790 5.361453 5.509807 5.682415 6.883975 7.392644 6.618984 5.752759
##        368      369      370      371      372      373      374      375
## 1 8.916963 7.876027 8.330037 8.374001 7.563523 8.532946 8.984585 9.736883
## 2 4.770107 4.939445 6.154059 6.280595 4.367286 5.917457 3.722610 4.767839
## 3 8.102215 6.662121 5.980344 5.981912 6.056314 6.266885 7.931335 8.798177
## 4 5.856186 5.544439 6.673537 6.801323 5.253873 6.587324 5.414656 6.304659
## 5 5.030692 6.544359 7.110024 7.285275 6.133890 6.680971 4.907474 5.412285
## 6 7.067897 6.260581 7.113702 7.203297 5.999323 7.191112 6.985946 7.809059
##        376      377      378      379      380       381      382      383
## 1 7.795568 7.924073 7.787937 8.275536 8.137870 12.821812 7.966471 8.185980
## 2 3.171317 2.549903 2.441746 3.030744 2.612679  9.894275 2.535543 2.500835
## 3 6.521129 6.722821 6.526672 7.136095 7.001182 12.116793 6.800516 7.087802
## 4 4.944969 4.652842 4.462833 5.083590 4.746519 11.163729 4.647065 4.555749
## 5 5.207964 4.521828 4.798660 4.679079 4.555128  9.663159 4.661952 4.529605
## 6 6.017927 6.001561 5.834679 6.394230 6.161471 11.755809 6.015300 6.112881
##        384      385      386      387      388      389      390      391
## 1 8.197628 9.349727 8.910330 9.193301 9.152365 8.907678 8.068366 7.677835
## 2 2.535837 4.207220 3.430304 3.965891 3.922688 3.476912 2.460378 2.173913
## 3 7.080988 8.494759 7.900222 8.245243 8.254546 7.913604 7.016902 6.584068
## 4 4.556169 5.912501 5.372451 5.774218 5.768068 5.325687 4.443905 4.162427
## 5 4.555957 4.593328 4.644399 4.813092 4.798740 4.585380 4.568138 4.613924
## 6 6.125147 7.394570 6.913418 7.236737 7.210917 6.892982 5.975128 5.602574
##        392      393      394      395      396      397      398      399
## 1 7.292592 8.487710 7.524344 7.763687 7.641959 7.677186 8.002371 9.767720
## 2 2.226899 2.983346 2.121055 2.265178 2.176869 2.197872 2.365545 4.881189
## 3 6.187448 7.518315 6.415998 6.699030 6.447121 6.503431 6.938797 8.851159
## 4 4.059557 4.886988 4.152838 4.352711 4.260815 4.238695 4.395922 6.683507
## 5 4.690817 4.584419 4.693783 4.560825 4.700448 4.730041 4.591516 5.349991
## 6 5.308181 6.426170 5.489378 5.726631 5.638596 5.653609 5.914268 7.989079
##        400      401      402      403      404      405       406      407
## 1 8.388325 8.804129 8.087911 7.825523 8.542363 9.552186 11.432606 9.054357
## 2 3.040490 3.471798 2.536220 2.216427 3.279024 4.910238  7.595875 3.994384
## 3 7.463474 7.776663 6.969235 6.627020 7.610294 8.716935 10.666205 8.166864
## 4 4.959465 5.497801 4.669753 4.375806 5.225734 6.673070  9.081383 5.594715
## 5 4.478721 4.713832 4.642624 4.544640 4.647198 5.160244  7.453981 4.877537
## 6 6.400821 6.900501 6.095533 5.820693 6.578583 7.840199 10.009989 7.043493
##        408      409      410       411      412      413      414
## 1 7.570396 7.942399 7.792350 10.375746 8.547474 9.825302 8.675535
## 2 2.751697 2.684071 3.512054  6.953620 4.401901 5.499975 4.000683
## 3 6.317911 6.868005 6.505072  9.624185 7.490223 8.978058 7.777474
## 4 4.354104 4.411296 4.984789  8.179599 5.753451 6.786214 5.592512
## 5 4.684219 4.359127 4.498617  6.248943 4.283169 4.939472 4.338544
## 6 5.605718 5.909119 6.091597  9.003486 6.898431 8.111524 6.840670
##         415      416      417      418       419      420      421
## 1 11.272810 9.378378 8.990931 9.186224 12.321200 8.708818 7.649405
## 2  6.897999 4.863694 4.682977 4.396530  8.940339 4.410853 2.275161
## 3 10.507271 8.404166 8.034930 8.321960 11.623093 7.789436 6.412841
## 4  8.419684 6.420781 6.131883 6.083046 10.281934 5.925977 4.353909
## 5  6.006137 4.335355 4.349290 4.150387  7.922425 4.248090 4.384831
## 6  9.673650 7.712449 7.339434 7.393740 11.103741 7.052418 5.696703
##        422      423      424      425      426      427      428      429
## 1 7.665560 7.356701 8.626415 8.441732 9.201143 8.265300 9.461813 8.241034
## 2 2.180745 2.508136 4.580701 4.667841 4.741929 4.569780 5.669296 3.716263
## 3 6.540748 6.403579 7.807594 7.858624 8.314551 7.743199 8.719330 7.368712
## 4 4.220529 4.151550 5.802264 5.736500 6.219861 5.681331 7.081609 5.224531
## 5 4.337641 4.332067 4.287058 4.467816 4.150667 4.462962 5.101391 3.944706
## 6 5.633333 5.359069 6.942560 6.733842 7.490506 6.578878 7.976041 6.437220
##        430      431      432      433      434      435      436      437
## 1 8.696259 7.931573 8.088359 7.540841 8.039230 8.323316 8.488298 8.855936
## 2 4.134503 3.794201 3.973307 3.782250 3.619313 3.721287 3.748221 4.457023
## 3 7.717744 7.082043 7.134695 6.790664 7.058495 7.351287 7.386206 7.874780
## 4 5.654346 5.081922 5.298499 4.904392 5.104864 5.332295 5.460661 5.994414
## 5 4.007002 4.145470 4.267862 4.462911 4.105578 3.959357 3.969580 4.147301
## 6 6.946892 6.173374 6.398175 5.806166 6.272329 6.545788 6.747217 7.180959
##        438      439      440      441      442      443      444      445
## 1 9.396533 9.435637 8.099988 8.384080 7.713189 7.607664 7.769558 8.369895
## 2 4.811409 4.695929 2.433801 2.937692 2.207332 2.192461 2.239337 2.871156
## 3 8.394236 8.484083 7.006532 7.350912 6.477458 6.351316 6.518089 7.288150
## 4 6.386947 6.327543 4.552303 5.079909 4.380978 4.219854 4.422982 4.931360
## 5 4.213632 4.342741 4.516857 4.509393 4.608229 4.755074 4.637509 3.918161
## 6 7.707796 7.700430 6.054304 6.446442 5.755888 5.609025 5.811139 6.418899
##        446      447      448      449      450      451      452      453
## 1 8.869383 7.692356 7.719083 7.662880 7.734779 8.619425 7.510000 7.420544
## 2 4.315338 2.223077 2.207460 2.126064 2.281549 4.610171 2.238968 2.096565
## 3 7.822260 6.503523 6.592742 6.535974 6.588139 7.643669 6.322861 6.315283
## 4 5.892565 4.313539 4.358067 4.262430 4.358030 5.922856 4.230132 4.095576
## 5 4.052052 4.377493 4.626360 4.619462 4.290924 4.436440 4.659548 4.698128
## 6 7.168870 5.714462 5.725384 5.655226 5.767098 7.013088 5.561251 5.419305
##        454      455      456      457      458      459      460      461
## 1 7.488797 8.615112 8.280416 8.613939 8.529698 7.413953 7.197961 7.441333
## 2 2.816811 4.549028 4.088681 4.450875 4.516870 2.377775 2.175860 2.581875
## 3 6.201144 7.628370 7.338629 7.770650 7.771267 6.463171 6.189770 6.350729
## 4 4.575975 5.918794 5.464389 5.749648 5.801327 4.266589 4.034746 4.354303
## 5 5.115979 4.321729 4.205509 4.252759 4.258883 4.236988 4.789795 4.385829
## 6 5.704355 7.016989 6.589745 6.913673 6.866815 5.484535 5.216250 5.566897
##        462      463      464      465      466      467      468      469
## 1 7.224008 7.129990 7.081032 6.761263 6.784776 8.163381 7.276556 7.067634
## 2 2.197525 2.207914 2.436963 2.400811 2.744376 4.323182 2.345760 2.548814
## 3 6.150612 6.113243 6.000826 5.995825 6.265945 7.375314 6.308918 6.388907
## 4 4.028929 4.046035 4.100801 3.940852 4.014623 5.454107 3.942868 4.149291
## 5 4.871657 4.859767 5.057480 4.982831 4.910892 4.372635 4.541440 4.648344
## 6 5.245377 5.175440 5.168781 4.825356 4.830816 6.492417 5.264318 5.133202
##        470      471      472      473      474      475      476      477
## 1 6.854434 6.816620 6.708850 6.594683 6.547902 7.501061 7.637258 7.206421
## 2 2.734118 2.311425 2.560930 2.499788 3.048901 2.434451 2.523020 2.223730
## 3 6.323801 5.996607 5.921288 5.766964 5.546301 6.666256 6.654666 6.151680
## 4 4.089454 3.742729 3.729722 3.771861 4.165060 4.053967 4.237950 3.945356
## 5 4.973919 4.971853 5.181002 5.161492 5.472082 4.527575 4.374497 4.862825
## 6 4.901491 4.816208 4.722700 4.659061 4.806139 5.415982 5.640980 5.208138
##        478      479      480      481      482      483      484      485
## 1 8.130462 7.407656 7.165587 6.328683 6.331364 6.329978 6.225603 6.407568
## 2 2.819877 2.149619 2.407795 2.841677 3.062382 3.305435 3.354835 3.007909
## 3 7.232034 6.384794 6.121775 5.803910 5.637316 5.575392 6.127800 6.133862
## 4 4.658359 4.056417 4.119396 3.815854 3.967622 4.139027 4.014627 3.945221
## 5 4.341229 4.588992 4.776866 5.387882 5.594453 5.786301 5.679794 5.329406
## 6 6.105211 5.382604 5.214375 4.428172 4.526780 4.616956 4.359692 4.497564
##        486      487      488      489      490      491      492      493
## 1 6.249662 6.708037 6.500512 7.746032 8.298286 8.670417 7.692851 7.224881
## 2 2.935648 2.355052 2.761430 3.339171 3.627365 4.033745 3.245201 3.298803
## 3 5.880484 6.022019 6.037518 6.749983 7.341242 7.743312 6.605526 6.225869
## 4 3.898792 3.748954 3.856913 4.067660 4.515760 4.922601 4.029950 3.853652
## 5 5.440584 4.972136 5.232320 5.468533 5.317298 5.364747 5.458144 5.624994
## 6 4.392934 4.727043 4.539202 5.655817 6.220009 6.633606 5.634401 5.208921
##        494      495      496      497      498      499      500      501
## 1 5.413752 5.273091 5.426681 6.011801 5.542505 5.414893 5.810165 5.688309
## 2 3.852454 3.986370 4.203245 3.717331 3.612856 3.666547 3.612745 3.521661
## 3 4.881230 4.787928 5.205773 5.359640 4.929451 4.699697 5.121802 4.887289
## 4 2.137355 2.335780 2.705197 2.322917 1.885295 1.925584 2.012924 1.880862
## 5 5.924585 6.035621 6.099197 5.645035 5.731891 5.820200 5.672489 5.683894
## 6 2.874711 2.815571 3.076628 3.570270 2.982151 2.867755 3.260115 3.150398
##        502      503      504      505      506
## 1 5.506603 5.730311 5.775209 5.732539 5.991382
## 2 4.294822 4.194855 4.555464 4.415418 4.255945
## 3 4.737543 4.960431 4.668497 4.734780 5.374542
## 4 2.309307 2.174338 2.764118 2.545516 2.378712
## 5 6.417801 6.310447 6.682914 6.547413 6.324684
## 6 3.056424 3.190441 3.518096 3.391335 3.469554
## 
## $totss
## [1] 748534.8
## 
## $withinss
## [1] 17974.37 26811.41 21548.41 22667.20 55771.90 25160.33
## 
## $tot.withinss
## [1] 169933.6
## 
## $betweenss
## [1] 578601.2
myclust<-data.frame(km_bs2$cluster)
boston_scaled2$clust<-km_bs2$cluster
head(boston_scaled2)
##         crim         zn      indus       chas        nox        rm
## 1 -0.4193669  0.2845483 -1.2866362 -0.2723291 -0.1440749 0.4132629
## 2 -0.4169267 -0.4872402 -0.5927944 -0.2723291 -0.7395304 0.1940824
## 3 -0.4169290 -0.4872402 -0.5927944 -0.2723291 -0.7395304 1.2814456
## 4 -0.4163384 -0.4872402 -1.3055857 -0.2723291 -0.8344581 1.0152978
## 5 -0.4120741 -0.4872402 -1.3055857 -0.2723291 -0.8344581 1.2273620
## 6 -0.4166314 -0.4872402 -1.3055857 -0.2723291 -0.8344581 0.2068916
##          age      dis        rad        tax    ptratio     black
## 1 -0.1198948 0.140075 -0.9818712 -0.6659492 -1.4575580 0.4406159
## 2  0.3668034 0.556609 -0.8670245 -0.9863534 -0.3027945 0.4406159
## 3 -0.2655490 0.556609 -0.8670245 -0.9863534 -0.3027945 0.3960351
## 4 -0.8090878 1.076671 -0.7521778 -1.1050216  0.1129203 0.4157514
## 5 -0.5106743 1.076671 -0.7521778 -1.1050216  0.1129203 0.4406159
## 6 -0.3508100 1.076671 -0.7521778 -1.1050216  0.1129203 0.4101651
##        lstat       medv clust
## 1 -1.0744990  0.1595278     6
## 2 -0.4919525 -0.1014239     6
## 3 -1.2075324  1.3229375     6
## 4 -1.3601708  1.1815886     6
## 5 -1.0254866  1.4860323     6
## 6 -1.0422909  0.6705582     6
lda.fit_bs2<-lda(clust~., data = boston_scaled2 )
lda.fit_bs2
## Call:
## lda(clust ~ ., data = boston_scaled2)
## 
## Prior probabilities of groups:
##          1          2          3          4          5          6 
## 0.10079051 0.19960474 0.09486166 0.20553360 0.12845850 0.27075099 
## 
## Group means:
##         crim          zn        indus       chas         nox          rm
## 1 -0.4149170  2.55535505 -1.228758914 -0.1951310 -1.21919439  0.78676843
## 2  0.3880377 -0.48724019  1.165421314 -0.2723291  0.98659851 -0.28553884
## 3 -0.3613809 -0.09419977 -0.474086929  1.5321752 -0.12487357  1.27068222
## 4 -0.3580718 -0.46023584 -0.003188584 -0.2723291 -0.09478548 -0.35414265
## 5  1.4172264 -0.48724019  1.069802298  0.4545202  1.34622349 -0.73713928
## 6 -0.4055840  0.02149547 -0.740804469 -0.2723291 -0.79649957  0.09099544
##          age        dis        rad        tax    ptratio       black
## 1 -1.4488239  1.7464736 -0.7048880 -0.5692695 -0.8353442  0.34924852
## 2  0.7651453 -0.7898745  1.1388129  1.2431405  0.6932747  0.04498348
## 3  0.2307707 -0.3386056 -0.4961654 -0.7220694 -1.1226766  0.32813467
## 4  0.4093998 -0.2612071 -0.5865335 -0.4342609  0.2608189  0.19191309
## 5  0.8557425 -0.9615698  1.2885597  1.2934457  0.4142248 -1.68787016
## 6 -0.8223904  0.7053125 -0.5694290 -0.7355910 -0.2013102  0.37698635
##        lstat       medv
## 1 -0.9773530  0.8760790
## 2  0.6734731 -0.5987824
## 3 -0.6138415  1.4407282
## 4  0.1508360 -0.2838601
## 5  1.1961180 -0.8078336
## 6 -0.5996059  0.2092896
## 
## Coefficients of linear discriminants:
##                 LD1         LD2         LD3         LD4         LD5
## crim     0.04811996 -0.28556378 -0.55488255  0.49400398  0.05329096
## zn      -0.13738829 -1.83004313  0.34546140 -0.26802062 -0.87758918
## indus    0.74925386 -0.10015651  0.61607026 -0.42031079  0.25109137
## chas     0.13287282 -0.13228082 -0.94523359 -0.16829634  0.04786106
## nox      1.21764057 -0.81216848 -0.12506389  0.27633410  0.13213424
## rm      -0.12060003 -0.04058521 -0.02502279 -0.75468374  0.21331834
## age      0.17397462  0.34382124 -0.07430813 -0.37956005 -0.95205471
## dis     -0.36273454 -0.54652248  0.11546588  0.26210162  0.59195828
## rad      0.61453519  0.40958433  0.29006265 -0.40963042  1.56473994
## tax      0.75124298 -1.03741454  0.22707980 -0.17126395 -0.61781814
## ptratio  0.36217649 -0.18603253  0.30060517  0.16017164 -0.53729844
## black   -0.27542772  0.27016025  0.77143821 -0.87012879  0.23445845
## lstat    0.48988940 -0.40861927 -0.53017288 -0.23295699 -0.06758426
## medv     0.22977036 -0.57759705 -0.86635437 -0.06977308 -0.10361245
## 
## Proportion of trace:
##    LD1    LD2    LD3    LD4    LD5 
## 0.7285 0.1498 0.0750 0.0298 0.0168
lda.arrows <- function(x, myscale = 1, arrow_heads = 0.1, color = "red", tex = 0.75, choices = c(1,2)){
  heads <- coef(x)
  arrows(x0 = 0, y0 = 0, 
         x1 = myscale * heads[,choices[1]], 
         y1 = myscale * heads[,choices[2]], col=color, length = arrow_heads)
  text(myscale * heads[,choices], labels = row.names(heads), 
       cex = tex, col=color, pos=3)
}
plot(lda.fit_bs2, dimen = 2)
lda.arrows(lda.fit_bs2, myscale = 3)

I think the dataset could be grouped more easily by a lower number of clusters, though I’m not sure what would be the minimum number. The top three most relevant variables according to our bi-plot are “zn”, “nox”, and “tax”.

Super.Bonus

Additional ways for visualising LDA:

library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:MASS':
## 
##     select
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
model_predictors <- dplyr::select(train, -crime)
dim(model_predictors)
## [1] 404  13
dim(lda.fit$scaling)
## [1] 13  3
matrix_product <- as.matrix(model_predictors) %*% lda.fit$scaling
matrix_product <- as.data.frame(matrix_product)
plot_ly(x = matrix_product$LD1, y = matrix_product$LD2, z = matrix_product$LD3, type= 'scatter3d', mode='markers', color = train$crim)
k_means_matpro <- kmeans(matrix_product, centers = 6, iter.max = 10, nstart = 1, trace=FALSE)
head(train)
##             zn      indus       chas        nox         rm        age
## 57   3.1573164 -1.5154874 -0.2723291 -1.2486880  0.1399989 -1.1678945
## 404 -0.4872402  1.0149946 -0.2723291  1.1935426 -1.3316418  0.9742880
## 297 -0.4872402  0.4056965 -0.2723291 -1.0156836  0.3762584 -0.6243557
## 29  -0.4872402 -0.4368257 -0.2723291 -0.1440749  0.2994029  0.9174474
## 306  0.9277053 -1.3055857 -0.2723291 -0.7136410  0.4716162 -0.3721252
## 479 -0.4872402  1.0149946 -0.2723291  0.5117892 -0.1418047  0.9991558
##            dis        rad        tax     ptratio     black       lstat
## 57   2.5609210 -0.8670245 -0.5650812 -0.53374719 0.4406159 -0.96387116
## 404 -0.9936043  1.6596029  1.5294129  0.80577843 0.4406159  0.99662169
## 297  1.0283264 -0.6373311 -0.7074831 -1.13422420 0.3962541 -0.73701413
## 29   0.3132707 -0.6373311 -0.6006817  1.17530274 0.3424724  0.02057632
## 306 -0.2018524 -0.2927910 -1.1050216 -0.02565127 0.4018404 -0.52135992
## 479 -0.7714940  1.6596029  1.5294129  0.80577843 0.2522154  0.75296043
##           medv    crime
## 57   0.2356387      low
## 404 -1.5475313     high
## 297  0.4965904      low
## 29  -0.4493595 med_high
## 306  0.6379392      low
## 479 -0.8625331     high
myclust <- NA
train$cl<-myclust
boston_scaled2$cl<-myclust
head(boston_scaled2)
##         crim         zn      indus       chas        nox        rm
## 1 -0.4193669  0.2845483 -1.2866362 -0.2723291 -0.1440749 0.4132629
## 2 -0.4169267 -0.4872402 -0.5927944 -0.2723291 -0.7395304 0.1940824
## 3 -0.4169290 -0.4872402 -0.5927944 -0.2723291 -0.7395304 1.2814456
## 4 -0.4163384 -0.4872402 -1.3055857 -0.2723291 -0.8344581 1.0152978
## 5 -0.4120741 -0.4872402 -1.3055857 -0.2723291 -0.8344581 1.2273620
## 6 -0.4166314 -0.4872402 -1.3055857 -0.2723291 -0.8344581 0.2068916
##          age      dis        rad        tax    ptratio     black
## 1 -0.1198948 0.140075 -0.9818712 -0.6659492 -1.4575580 0.4406159
## 2  0.3668034 0.556609 -0.8670245 -0.9863534 -0.3027945 0.4406159
## 3 -0.2655490 0.556609 -0.8670245 -0.9863534 -0.3027945 0.3960351
## 4 -0.8090878 1.076671 -0.7521778 -1.1050216  0.1129203 0.4157514
## 5 -0.5106743 1.076671 -0.7521778 -1.1050216  0.1129203 0.4406159
## 6 -0.3508100 1.076671 -0.7521778 -1.1050216  0.1129203 0.4101651
##        lstat       medv clust cl
## 1 -1.0744990  0.1595278     6 NA
## 2 -0.4919525 -0.1014239     6 NA
## 3 -1.2075324  1.3229375     6 NA
## 4 -1.3601708  1.1815886     6 NA
## 5 -1.0254866  1.4860323     6 NA
## 6 -1.0422909  0.6705582     6 NA
head(train)
##             zn      indus       chas        nox         rm        age
## 57   3.1573164 -1.5154874 -0.2723291 -1.2486880  0.1399989 -1.1678945
## 404 -0.4872402  1.0149946 -0.2723291  1.1935426 -1.3316418  0.9742880
## 297 -0.4872402  0.4056965 -0.2723291 -1.0156836  0.3762584 -0.6243557
## 29  -0.4872402 -0.4368257 -0.2723291 -0.1440749  0.2994029  0.9174474
## 306  0.9277053 -1.3055857 -0.2723291 -0.7136410  0.4716162 -0.3721252
## 479 -0.4872402  1.0149946 -0.2723291  0.5117892 -0.1418047  0.9991558
##            dis        rad        tax     ptratio     black       lstat
## 57   2.5609210 -0.8670245 -0.5650812 -0.53374719 0.4406159 -0.96387116
## 404 -0.9936043  1.6596029  1.5294129  0.80577843 0.4406159  0.99662169
## 297  1.0283264 -0.6373311 -0.7074831 -1.13422420 0.3962541 -0.73701413
## 29   0.3132707 -0.6373311 -0.6006817  1.17530274 0.3424724  0.02057632
## 306 -0.2018524 -0.2927910 -1.1050216 -0.02565127 0.4018404 -0.52135992
## 479 -0.7714940  1.6596029  1.5294129  0.80577843 0.2522154  0.75296043
##           medv    crime cl
## 57   0.2356387      low NA
## 404 -1.5475313     high NA
## 297  0.4965904      low NA
## 29  -0.4493595 med_high NA
## 306  0.6379392      low NA
## 479 -0.8625331     high NA
rownames(train)
##   [1] "57"  "404" "297" "29"  "306" "479" "435" "496" "398" "243" "331"
##  [12] "459" "284" "224" "344" "213" "256" "179" "70"  "262" "61"  "290"
##  [23] "206" "202" "337" "114" "21"  "483" "323" "240" "333" "287" "365"
##  [34] "384" "230" "443" "482" "223" "315" "142" "12"  "24"  "302" "232"
##  [45] "233" "289" "126" "502" "170" "37"  "397" "95"  "248" "157" "480"
##  [56] "497" "498" "194" "52"  "452" "264" "216" "160" "456" "426" "282"
##  [67] "6"   "332" "159" "209" "31"  "245" "55"  "166" "92"  "440" "267"
##  [78] "491" "235" "316" "354" "276" "485" "65"  "147" "433" "254" "381"
##  [89] "358" "91"  "499" "429" "117" "277" "62"  "281" "133" "127" "25" 
## [100] "53"  "81"  "139" "22"  "342" "266" "204" "347" "15"  "319" "338"
## [111] "83"  "150" "385" "191" "71"  "109" "244" "48"  "469" "370" "336"
## [122] "103" "350" "220" "163" "477" "60"  "288" "424" "183" "75"  "372"
## [133] "226" "318" "339" "280" "66"  "330" "500" "234" "123" "361" "495"
## [144] "286" "229" "451" "453" "268" "395" "184" "367" "102" "247" "205"
## [155] "17"  "478" "54"  "260" "122" "363" "215" "434" "74"  "488" "311"
## [166] "446" "177" "322" "425" "175" "274" "265" "228" "359" "255" "45" 
## [177] "490" "129" "153" "279" "186" "283" "203" "59"  "188" "128" "237"
## [188] "300" "67"  "417" "317" "84"  "200" "107" "47"  "394" "10"  "19" 
## [199] "275" "189" "172" "252" "187" "101" "88"  "310" "351" "161" "38" 
## [210] "401" "1"   "504" "63"  "14"  "494" "68"  "249" "484" "301" "291"
## [221] "36"  "121" "467" "138" "327" "141" "212" "90"  "132" "7"   "304"
## [232] "402" "356" "82"  "389" "99"  "58"  "137" "335" "27"  "454" "124"
## [243] "197" "362" "340" "155" "168" "64"  "492" "455" "253" "442" "111"
## [254] "432" "236" "225" "176" "406" "410" "89"  "26"  "441" "259" "375"
## [265] "73"  "250" "105" "251" "49"  "210" "76"  "28"  "218" "383" "422"
## [276] "87"  "349" "379" "334" "98"  "321" "430" "41"  "364" "377" "148"
## [287] "380" "296" "413" "405" "393" "145" "18"  "108" "295" "309" "487"
## [298] "299" "242" "192" "34"  "135" "93"  "271" "115" "106" "307" "445"
## [309] "30"  "39"  "324" "238" "414" "198" "151" "51"  "241" "466" "308"
## [320] "387" "136" "419" "493" "501" "470" "16"  "130" "326" "171" "185"
## [331] "149" "438" "411" "32"  "174" "116" "239" "428" "144" "427" "449"
## [342] "43"  "278" "46"  "2"   "294" "292" "390" "366" "420" "23"  "437"
## [353] "169" "460" "392" "143" "162" "431" "325" "182" "382" "158" "444"
## [364] "5"   "369" "473" "118" "110" "416" "353" "341" "246" "193" "464"
## [375] "4"   "357" "180" "211" "465" "80"  "418" "415" "458" "173" "412"
## [386] "468" "156" "476" "69"  "152" "450" "96"  "258" "33"  "423" "312"
## [397] "113" "146" "348" "207" "3"   "439" "376" "56"
rownames(boston_scaled2)
##   [1] "1"   "2"   "3"   "4"   "5"   "6"   "7"   "8"   "9"   "10"  "11" 
##  [12] "12"  "13"  "14"  "15"  "16"  "17"  "18"  "19"  "20"  "21"  "22" 
##  [23] "23"  "24"  "25"  "26"  "27"  "28"  "29"  "30"  "31"  "32"  "33" 
##  [34] "34"  "35"  "36"  "37"  "38"  "39"  "40"  "41"  "42"  "43"  "44" 
##  [45] "45"  "46"  "47"  "48"  "49"  "50"  "51"  "52"  "53"  "54"  "55" 
##  [56] "56"  "57"  "58"  "59"  "60"  "61"  "62"  "63"  "64"  "65"  "66" 
##  [67] "67"  "68"  "69"  "70"  "71"  "72"  "73"  "74"  "75"  "76"  "77" 
##  [78] "78"  "79"  "80"  "81"  "82"  "83"  "84"  "85"  "86"  "87"  "88" 
##  [89] "89"  "90"  "91"  "92"  "93"  "94"  "95"  "96"  "97"  "98"  "99" 
## [100] "100" "101" "102" "103" "104" "105" "106" "107" "108" "109" "110"
## [111] "111" "112" "113" "114" "115" "116" "117" "118" "119" "120" "121"
## [122] "122" "123" "124" "125" "126" "127" "128" "129" "130" "131" "132"
## [133] "133" "134" "135" "136" "137" "138" "139" "140" "141" "142" "143"
## [144] "144" "145" "146" "147" "148" "149" "150" "151" "152" "153" "154"
## [155] "155" "156" "157" "158" "159" "160" "161" "162" "163" "164" "165"
## [166] "166" "167" "168" "169" "170" "171" "172" "173" "174" "175" "176"
## [177] "177" "178" "179" "180" "181" "182" "183" "184" "185" "186" "187"
## [188] "188" "189" "190" "191" "192" "193" "194" "195" "196" "197" "198"
## [199] "199" "200" "201" "202" "203" "204" "205" "206" "207" "208" "209"
## [210] "210" "211" "212" "213" "214" "215" "216" "217" "218" "219" "220"
## [221] "221" "222" "223" "224" "225" "226" "227" "228" "229" "230" "231"
## [232] "232" "233" "234" "235" "236" "237" "238" "239" "240" "241" "242"
## [243] "243" "244" "245" "246" "247" "248" "249" "250" "251" "252" "253"
## [254] "254" "255" "256" "257" "258" "259" "260" "261" "262" "263" "264"
## [265] "265" "266" "267" "268" "269" "270" "271" "272" "273" "274" "275"
## [276] "276" "277" "278" "279" "280" "281" "282" "283" "284" "285" "286"
## [287] "287" "288" "289" "290" "291" "292" "293" "294" "295" "296" "297"
## [298] "298" "299" "300" "301" "302" "303" "304" "305" "306" "307" "308"
## [309] "309" "310" "311" "312" "313" "314" "315" "316" "317" "318" "319"
## [320] "320" "321" "322" "323" "324" "325" "326" "327" "328" "329" "330"
## [331] "331" "332" "333" "334" "335" "336" "337" "338" "339" "340" "341"
## [342] "342" "343" "344" "345" "346" "347" "348" "349" "350" "351" "352"
## [353] "353" "354" "355" "356" "357" "358" "359" "360" "361" "362" "363"
## [364] "364" "365" "366" "367" "368" "369" "370" "371" "372" "373" "374"
## [375] "375" "376" "377" "378" "379" "380" "381" "382" "383" "384" "385"
## [386] "386" "387" "388" "389" "390" "391" "392" "393" "394" "395" "396"
## [397] "397" "398" "399" "400" "401" "402" "403" "404" "405" "406" "407"
## [408] "408" "409" "410" "411" "412" "413" "414" "415" "416" "417" "418"
## [419] "419" "420" "421" "422" "423" "424" "425" "426" "427" "428" "429"
## [430] "430" "431" "432" "433" "434" "435" "436" "437" "438" "439" "440"
## [441] "441" "442" "443" "444" "445" "446" "447" "448" "449" "450" "451"
## [452] "452" "453" "454" "455" "456" "457" "458" "459" "460" "461" "462"
## [463] "463" "464" "465" "466" "467" "468" "469" "470" "471" "472" "473"
## [474] "474" "475" "476" "477" "478" "479" "480" "481" "482" "483" "484"
## [485] "485" "486" "487" "488" "489" "490" "491" "492" "493" "494" "495"
## [496] "496" "497" "498" "499" "500" "501" "502" "503" "504" "505" "506"
train$cl <- boston_scaled2$clust[match(rownames(train), rownames(boston_scaled2))]
head(train)
##             zn      indus       chas        nox         rm        age
## 57   3.1573164 -1.5154874 -0.2723291 -1.2486880  0.1399989 -1.1678945
## 404 -0.4872402  1.0149946 -0.2723291  1.1935426 -1.3316418  0.9742880
## 297 -0.4872402  0.4056965 -0.2723291 -1.0156836  0.3762584 -0.6243557
## 29  -0.4872402 -0.4368257 -0.2723291 -0.1440749  0.2994029  0.9174474
## 306  0.9277053 -1.3055857 -0.2723291 -0.7136410  0.4716162 -0.3721252
## 479 -0.4872402  1.0149946 -0.2723291  0.5117892 -0.1418047  0.9991558
##            dis        rad        tax     ptratio     black       lstat
## 57   2.5609210 -0.8670245 -0.5650812 -0.53374719 0.4406159 -0.96387116
## 404 -0.9936043  1.6596029  1.5294129  0.80577843 0.4406159  0.99662169
## 297  1.0283264 -0.6373311 -0.7074831 -1.13422420 0.3962541 -0.73701413
## 29   0.3132707 -0.6373311 -0.6006817  1.17530274 0.3424724  0.02057632
## 306 -0.2018524 -0.2927910 -1.1050216 -0.02565127 0.4018404 -0.52135992
## 479 -0.7714940  1.6596029  1.5294129  0.80577843 0.2522154  0.75296043
##           medv    crime cl
## 57   0.2356387      low  1
## 404 -1.5475313     high  2
## 297  0.4965904      low  6
## 29  -0.4493595 med_high  4
## 306  0.6379392      low  6
## 479 -0.8625331     high  2
nrow(train)
## [1] 404
plot_ly(x = matrix_product$LD1, y = matrix_product$LD2, z = matrix_product$LD3, type = "scatter3d", mode="markers", color = train$cl)

In light of my research, clustering made with K-means have turned out to be more informative than thes one based on crime classes.